转自:http://hi.baidu.com/zijie410/blog/item/4fc216c33201c10d0ef477e9.html 软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果:
actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:
下面以搜索为例,演示一个实例,修改main.xml如下:
Xml代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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"/>
- </LinearLayout>
-
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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"/>
- </LinearLayout>
复制代码
修改HelloEditText如下:
- package com.flysnow;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- 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);
- 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;
- }
- });
- }
- }
-
- package com.flysnow;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- 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);
- 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;
- }
- });
- }
- }
复制代码
运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。
EditText的取值、全选、部分选择、获取选中文本
下面通过一个例子来演示EditText的取值、全选、部分选择和获取选中文本.main.xml修改如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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>
-
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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修改如下:
- 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;
- /**
- * EditText演示
- * @author 飞雪无情
- * @since 2010-11-29
- */
- 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) {
- Toast.makeText(HelloEditText.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
- }
- });
- //让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();
- }
- });
- }
- /**
- * 交换两个索引
- * @param start 开始索引
- * @param end 结束索引
- */
- protected void switchIndex(int start, int end) {
- int temp=start;
- start=end;
- end=temp;
- }
- }
-
- 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;
- /**
- * EditText演示
- * @author 飞雪无情
- * @since 2010-11-29
- */
- 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) {
- Toast.makeText(HelloEditText.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
- }
- });
- //让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();
- }
- });
- }
- /**
- * 交换两个索引
- * @param start 开始索引
- * @param end 结束索引
- */
- protected void switchIndex(int start, int end) {
- int temp=start;
- start=end;
- end=temp;
- }
- }
复制代码
运行效果如下: 可以通过输入文字和点击下面的按钮测试。 |