一、android 软件盘事件响应 在android中,有时需要对EditText实现软件盘监听的场景。当android按下软键盘的时候,响应完成、发送、搜索或者其他事件。 Google 提供了 EditorInfo、 KeyEvent 的一些方法,能够实现我们需要的功能。详细可研究:EditorInfo.class 和 KeyEvent.class. 输入回车键隐藏输入键盘的方法:
如果布局中包含多个EditText,可以为每个EditText控件设置android:singleLine=”true”,弹出的软盘输入法中回车键为next,直到最后一个获取焦点后显示为Done。点击Done后,隐藏软键输入盘。将EditText的imeOptions属性设置android:imeOptions=”actionDone”,则不管是不是最后一个EditText,点击回车键即隐藏输入法。
监听Enter的事件,编写Enter的事件响应。设置文本框的OnKeyListener,当keyCode ==KeyEvent.KEYCODE_ENTER的时候,表明Enter键被按下,就可以编写自己事件响应功能了。
XML文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案。
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 一个放大镜
actionSend : Send
actionNext : Next
actionDone : Done,隐藏软键盘,即使不是最后一个文本输入框
通过修改 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 效果:
JAVA代码:
1 2 | |
添加监听事件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
但是,如果手机的输入法不是内置输入法,而是其他第三方输入法,那么可能会发生软件盘回车键无响应的问题。为了防止该类事情,则增加红色部分,响应的其KeyEvent。 这时候需要在代码中添加事件响应。
inputKey = (EditText) findViewById(R.id.contactSearch_editText);
inputKey.addTextChangedListener(watcher);
inputKey.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) {
handler.post(updateView);
return true;
}
return false;
}
});
//响应键盘内容
public TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2,int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
handler.post(updateView);
}
};
二、android 输入类型 根据要输入的内容展现相应的软件盘,可通过修改 android:inputType 来实现。 这是一些常用的输入类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
密码框属性 android:password="true" 让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。 纯数字 android:numeric="true" 让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入 仅允许 android:capitalize="haoTest" 仅允许接受输入haoTest,一般用于密码验证 android:editable="false" 设置EditText不可编辑 android:singleLine="true" 强制输入的内容在单行 android:ellipsize="end" 自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时 (部分属性参考网络:http://blog.csdn.net/lushengchu_luis/article/details/8699791、http://www.eoeandroid.com/thread-313140-1-1.html) 转载请注明出处:http://www.cnblogs.com/haochuang/