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);


Android修改输入法Enter的文本_第1张图片Android修改输入法Enter的文本_第2张图片Android修改输入法Enter的文本_第3张图片

上面效果出来了,那么我们能不能来自定义这个按钮的事件呢?答案是肯定的:关键代码:
 <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 触摸手指动作放大和缩小图片
  2. Android如何避免输入法弹出时遮挡住按钮或输入框
  3. Android:EditText屏蔽输入法弹窗
  4. Android:UI设置-横竖屏转换、输入法键盘相关设置
  5. android EditText设置弹出数字输入法键盘
  6. android输入法手势程序源码
  7. Android如何横屏, 全屏, 背景灯常量

随机推荐

  1. SQL Server 数据库索引其索引的小技巧
  2. 设置SQLServer数据库中某些表为只读的多
  3. T-SQL问题解决集锦 数据加解密全集
  4. SQL Server 日期相关资料详细介绍
  5. SQL Server错误代码大全及解释(留着备用)
  6. MSSQL安全设置的具体步骤和方法小结
  7. MSSQL 基本语法及实例操作语句
  8. ROW_NUMBER SQL Server 2005的LIMIT功能
  9. SQL SERVER数据操作类代码
  10. 用SQL统计SQLServe表存储空间大小的代码