Android修改输入法Enter的文本
来源:互联网 发布:nba2k科沃尔数据 编辑:程序博客网 时间:2024/05/21 21:44
EditText设置Enter的文字有以下属性:
一:布局设法(android:imeOptions="actionSearch" android:singleLine="true"):
android:singleLine="true"这个必须要加,不然不会生效。
- 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
二:代码设法:
et.setSingleLine();这个必须要加,不然不会生效。
- et1.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
- et1.setImeOptions(EditorInfo.IME_ACTION_NONE);
- et1.setImeOptions(EditorInfo.IME_ACTION_GO);
- et1.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
- et1.setImeOptions(EditorInfo.IME_ACTION_SEND);
- et1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
- 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;}}
偷懒?看不懂?不想写?我懂你——>>源码
0 0
- Android修改输入法Enter的文本
- android enter 隐藏输入法
- 修改输入法键盘的enter为 “搜索”字样
- 修改Android虚拟机的输入法
- Android修改输入法的回车键
- EditText修改软键盘输入法的Enter键的按钮文字
- Android的文本和输入---创建输入法
- 关于输入法键盘的Enter键
- Android EditText按下输入法软件的enter按键,实现跳到下一个EditText等
- Android 默认输入法的修改方法
- Android修改默认的键盘输入法
- Android 修改输入法的输入语言
- Android系统默认输入法的修改
- Android系统默认输入法的修改
- Android 修改默认输入法
- android修改默认输入法
- Android 修改默认输入法
- 修改android默认输入法
- iPhone屏幕尺寸、分辨率及适配
- Linux学习笔记-----Linux服务管理----RPM服务管理---基于xinetd服务的服务管理
- 友盟微社区2.1版本的接入
- 20 吝啬的国度【邻接表+队列】
- zzuli OJ 1007: 鸡兔同笼
- Android修改输入法Enter的文本
- 获取bitmap的大小
- [机器学习实战]-决策树
- 词Ci首页效果实现介绍及源码分享
- 调用webservice的方式解析
- 现Linux发行版分类
- ORA-12547: TNS:lost contact
- Android Fragment——与Activity生命周期的协调
- 设计模式-策略模式