您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页Android开发—EditText

Android开发—EditText

来源:华佗小知识
EditText的取值、全选、部分选择、获取选中文本

下面通过一个例子来演示EditText的取值、全选、部分选择和获取选中文本.main.xml修改如下: Xml代码

< ?xml version=\"1.0\" encoding=\"utf-8\"?> < LinearLayout xmlns:andro android:orientation=\"vertical\" android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\" >

< EditText

android:id=\"@+id/edit_text\" android:layout_width=\"fill_parent\"

android:layout_height=\"wrap_content\" android:imeOptions=\"actionSearch\"/> < Button

android:id=\"@+id/btn_get_value\" android:text=\"取值\"

android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"/> < Button

android:id=\"@+id/btn_all\" android:text=\"全选\"

android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"/> < Button

android:id=\"@+id/btn_select\" android:text=\"从第2个字符开始选择\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"/> < Button

android:id=\"@+id/btn_get_select\" android:text=\"获取选中文本\"

android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"/>

< /LinearLayout> HelloEditText修改如下: Java代码

package com.flysnow;

import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.Selection; import android.view.KeyEvent; import android.view.View;

import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

import android.widget.TextView; import android.widget.Toast;

import android.widget.TextView.OnEditorActionListener;

public class HelloEditText extends Activity { /** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

final EditText editText=(EditText)findViewById(R.id.edit_text); //监听回车键

editText.setOnEditorActionListener(new OnEditorActionListener() { @Override

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

Toast.makeText(HelloEditText.this,

String.valueOf(actionId),

Toast.LENGTH_SHORT).show(); return false; } });

//获取EditText文本

Button getValue=(Button)findViewById(R.id.btn_get_value);

getValue.setOnClickListener(new OnClickListener() { @Override

public void onClick(View v) { } });

//让EditText全选

Button all=(Button)findViewById(R.id.btn_all); all.setOnClickListener(new OnClickListener() { @Override

public void onClick(View v) { editText.selectAll(); } });

//从第2个字符开始选择EditText文本

Button select=(Button)findViewById(R.id.btn_select); select.setOnClickListener(new OnClickListener() { @Override

public void onClick(View v) {

Editable editable=editText.getText();

Selection.setSelection(editable, 1,editable.length()); } });

//获取选中的文本

Button getSelect=(Button)findViewById(R.id.btn_get_select); getSelect.setOnClickListener(new OnClickListener() { @Override

public void onClick(View v) {

int start=editText.getSelectionStart(); int end=editText.getSelectionEnd();

CharSequence selectText=editText.getText().subSequence(start, end); Toast.makeText(HelloEditText.this, selectText, Toast.LENGTH_SHORT).show(); } });

Toast.makeText(HelloEditText.this,

editText.getText().toString(),

Toast.LENGTH_SHORT).show();

} /**

* 交换两个索引

* @param start 开始索引 * @param end 结束索引 */

protected void switchIndex(int start, int end) { int temp=start; start=end; end=temp; } }

运行效果如下:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务