手机软键盘按键监听
2017-03-24 13:57
127 查看
转自:http://blog.csdn.net/zhufuing/article/details/18964725/
前言:
我们在Android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“Go”按键加载url页面;在点击搜索框的时候,点击右下角的search符号键可以进行搜索;或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作。
效果图:
function 1:
重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中监听KeyEventKey.KEYCODE_ENTER键(右下角确定键),当此键按下的时候,隐藏输入法软键盘,设置edittext内容和加载webview内容。[java] view
plain copy
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
/*隐藏软键盘*/
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputMethodManager.isActive()){
inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
}
edittext.setText("success");
webview.loadUrl(URL);
return true;
}
return super.dispatchKeyEvent(event);
}
function 2:
重写dispatchKeyEvent(KeyEvent event)的方法感觉有点用牛刀的感觉,因为我们非常可能在这个方法中进行其他任务,所以我们可以使用OnKeyListener的方法来监听软键盘按键。[java] view
plain copy
private OnKeyListener onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
/*隐藏软键盘*/
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputMethodManager.isActive()){
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
edittext.setText("success");
webview.loadUrl(URL);
return true;
}
return false;
}
};
[java] view
plain copy
edittext.setOnKeyListener(onKeyListener);
function 3:
第三种方法我认为可以帮助程序员更精确的判断右下角按键情况,以便应对更加复杂的情况。它可以帮助程序员依据当前邮件下为“GO”,“done”,“search”键的情况下做出更细分的操作。[java] view
plain copy
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判断是否是“GO”键*/
if(actionId == EditorInfo.IME_ACTION_GO){
/*隐藏软键盘*/
InputMethodManager imm = (InputMethodManager) v
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(
v.getApplicationWindowToken(), 0);
}
edittext.setText("success");
webview.loadUrl(URL);
return true;
}
return false;
}
});
改变软键盘右下角确定键样式:
软键盘输入法的按键并不是一成不变的,例如它的右下角的“确定”键,在有搜索框的时候就会变成带搜索图标的按键,在浏览器地址栏的时候则会变成“GO”键,我们在写App的时候也可能根据情况的不同设置输入法的“确定”键,改变方法就是给EditText控件的imeOptions属性设置成不同的值(此时Enter键可以显示不同的文字和图案)。[html] view
plain copy
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionSearch"/>
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 放大镜
actionSend : Send
actionNext : Next
actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框
题外话:
我在写这个demo的时候,发现了webview的一个问题,就是直接使用webview.load(url)方法会在手机上面弹出系统浏览器来访问url链接,而不是我们设置的webview,我找到的解决办法就是使用webview.setWebViewClient(....)的方法来确保url会在activity的webview上面加载。
demo下载地址:
http://download.csdn.net/detail/zhufuing/6903671转自:http://blog.csdn.net/lastdream/article/details/24365633
虽然通常输入法软键盘右下角会是回车按键
但我们经常会看到点击不同的编辑框,输入法软键盘右下角会有不同的图标
点击浏览器网址栏的时候,输入法软键盘右下角会变成“Go”或“前往”
而我们点击Google搜索框,输入法软键盘右下角会变成 放大镜 或者“搜索”
而决定这个图标的变换的参数就是EditText中的 Android:imeOptions
android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等,这些意思都很明显
[html] view
plain copy
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
/>
而其在Java代码中对应的值为EditorInfo.IME_ACTION_XXX
在代码中通过editText.setOnEditorActionListener方法添加相应的监听,因为有些action是需要在代码中添加具体的相关操作的
[java] view
plain copy
EditText editText = (EditText) contentView.findViewById(R.id.editText);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(getActivity(), "1111111",Toast.LENGTH_SHORT).show();
}
return false;
}
});
然而当我们设置这一切后,却发现点击输入框,输入法键盘完全没变化,还是回车键
这并不是上面的属性和方法无效,而是我们还需要设置别的属性来使它们生效
经过试验 设置下面两个属性中的一个即可使这个属性生效(应该还有其他的属性也可以,没去试验)
1 将singleLine设置为true
2 将inputType设置为text
[html] view
plain copy
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:singleLine="true"
android:inputType="text"
/>
java代码设置
[java] view
plain copy
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setSingleLine(true);
相关文章推荐
- Android 按键的监听
- 监听手机的耳机按键实现你的后台播放
- Android如何监听蓝牙耳机的按键事件
- 游戏专用的stage按键监听
- Android 监听系统虚拟导航栏按键
- Android监听按键返回键
- android耳机中间按键(中键)监听
- Android常用之返回按键监听
- Android App监听软键盘按键的三种方式 转
- DialogFragment 监听按键事件的方法(onkeydown)
- Android App监听软键盘按键的三种方式
- javascript 组合按键事件监听实现代码
- Android通过Intent.ACTION_CLOSE_SYSTEM_DIALOGS监听Home按键消息
- JavaScript 监听textarea中按键事件
- Android 监听按键信息的时候会出现两次响应的解决办法
- Android 更改软键盘右下角按键的样式以及监听此键的方法
- cocos2d-x在Linux平台上添加按键监听
- Unigine 监听键盘事件和模拟按键
- Android 监听系统软键盘按键的三种方式
- Android App监听软键盘按键的三种方式