EditText设置Enter的文字有以下属性:

一:布局设法(android:imeOptions="actionSearch"android:singleLine="true"):
android:singleLine="true"这个必须要加,不然不会生效。
  1. actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

二:代码设法:
et.setSingleLine();这个必须要加,不然不会生效。
  1. et1.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
  2. et1.setImeOptions(EditorInfo.IME_ACTION_NONE);
  3. et1.setImeOptions(EditorInfo.IME_ACTION_GO);
  4. et1.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
  5. et1.setImeOptions(EditorInfo.IME_ACTION_SEND);
  6. et1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
  7. et1.setImeOptions(EditorInfo.IME_ACTION_DONE);




上面效果出来了,那么我们能不能来自定义这个按钮的事件呢?答案是肯定的: 关键代码:
 <EditText        android:id="@+id/et7"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="搜索"        android:imeOptions="actionSearch"        android:singleLine="true" />



et7.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {boolean isClick = event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER;if (actionId == EditorInfo.IME_ACTION_SEARCH || isClick) {return true;}return false;}});

下面是完整代码: 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical" >    <EditText        android:id="@+id/et1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="未指定"        android:imeOptions="actionUnspecified"        android:singleLine="true" />    <EditText        android:id="@+id/et2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="没有动作"        android:imeOptions="actionNone"        android:singleLine="true" />    <EditText        android:id="@+id/et3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="去往"        android:imeOptions="actionGo"        android:singleLine="true" />    <EditText        android:id="@+id/et4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="搜索"        android:imeOptions="actionSearch"        android:singleLine="true" />    <EditText        android:id="@+id/et5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="发送"        android:imeOptions="actionSend"        android:singleLine="true" />    <EditText        android:id="@+id/et6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="下一个"        android:imeOptions="actionNext"        android:singleLine="true" />    <EditText        android:id="@+id/et7"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:ems="10"        android:hint="完成"        android:imeOptions="actionDone"        android:singleLine="true" /></LinearLayout>


代码文件:
package com.xiaoxi.entertext;import java.util.HashMap;import java.util.Map;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Window;import android.view.inputmethod.EditorInfo;import android.widget.EditText;import android.widget.TextView;import android.widget.TextView.OnEditorActionListener;@SuppressLint({ "UseSparseArrays", "CutPasteId" })public class MainActivity extends Activity implements OnEditorActionListener {// EditText通过设置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_DONEprivate Map<Integer, Boolean> enterTypeMap;private Map<Integer, String> enterTextMap;private EditText et1;private EditText et2;private EditText et3;private EditText et4;private EditText et5;private EditText et6;private EditText et7;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initData();setListener();}private void initView() {et1 = (EditText) findViewById(R.id.et1);et2 = (EditText) findViewById(R.id.et2);et3 = (EditText) findViewById(R.id.et3);et4 = (EditText) findViewById(R.id.et4);et5 = (EditText) findViewById(R.id.et5);et6 = (EditText) findViewById(R.id.et6);et7 = (EditText) findViewById(R.id.et7);}private void initData() {enterTypeMap = new HashMap<Integer, Boolean>();enterTypeMap.put(EditorInfo.IME_ACTION_UNSPECIFIED, true);// 未指定enterTypeMap.put(EditorInfo.IME_ACTION_NONE, true);// 没有动作enterTypeMap.put(EditorInfo.IME_ACTION_GO, true);// 去往enterTypeMap.put(EditorInfo.IME_ACTION_SEARCH, true);// 搜索enterTypeMap.put(EditorInfo.IME_ACTION_SEND, true);// 发送enterTypeMap.put(EditorInfo.IME_ACTION_NEXT, true);// 下一个enterTypeMap.put(EditorInfo.IME_ACTION_DONE, true);// 完成enterTextMap = new HashMap<Integer, String>();enterTextMap.put(EditorInfo.IME_ACTION_UNSPECIFIED, "未指定");enterTextMap.put(EditorInfo.IME_ACTION_NONE, "没有动作");enterTextMap.put(EditorInfo.IME_ACTION_GO, "去往");enterTextMap.put(EditorInfo.IME_ACTION_SEARCH, "搜索");enterTextMap.put(EditorInfo.IME_ACTION_SEND, "发送");enterTextMap.put(EditorInfo.IME_ACTION_NEXT, "下一个");enterTextMap.put(EditorInfo.IME_ACTION_DONE, "完成");}private void setListener() {et1.setOnEditorActionListener(this);et2.setOnEditorActionListener(this);et3.setOnEditorActionListener(this);et4.setOnEditorActionListener(this);et5.setOnEditorActionListener(this);et6.setOnEditorActionListener(this);et7.setOnEditorActionListener(this);}/** * 监听输入法Enter的单击事件 */@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {String searchStr = v.getText().toString().trim();Log.i("TEST", "输入的内容---" + searchStr);if (enterTypeMap.get(actionId) || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {Log.i("TEST", "文本框的类型---" + enterTextMap.get(actionId));return true;}return false;}}

偷懒?看不懂?不想写?我懂你——>>源码

更多相关文章

  1. Android(安卓)Launcher label和Main Activity保持不一致
  2. Android去掉状态栏和标题栏的两种方式
  3. Android(安卓)Studio 之 View组件常用属性及其对应的编程接口
  4. Android中使用log4j
  5. android EditText inputType 及 android:imeOptions=”actionDon
  6. android EditText inputType 及 android:imeOptions=”actionDon
  7. NDK版本与Android固件要求对应表
  8. Android(安卓)API版本对应Android系统版本及内核版本和代号
  9. Android(安卓)Intent 教程

随机推荐

  1. Android(安卓)ApiDemos 学习——时间日期
  2. android UI进阶之弹窗的使用(2)--实现通讯
  3. The method setOnClickListener(View.OnC
  4. 使用gdb远程调试android native程序
  5. android sdk+eclipse+adt 配置与开发
  6. Android打开系统设置界面
  7. android2.3 api demo 学习系列(4)--App/Act
  8. Android(安卓)布局
  9. Android(安卓)OTA 升级之二:脚本 ota_from
  10. Android(安卓)Adapter的应用