Android App监听软键盘按键的三种方式
2015-06-08 16:33
441 查看
分类: Android
2014-02-08 10:35 14696人阅读 评论(7) 收藏 举报
Androidsoftboard监听软键
[java] view
plaincopy
@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);
}
[java] view
plaincopy
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
plaincopy
edittext.setOnKeyListener(onKeyListener);
[java] view
plaincopy
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;
}
});
[html] view
plaincopy
<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,确定/完成,隐藏软键盘,即使不是最后一个文本输入框
2014-02-08 10:35 14696人阅读 评论(7) 收藏 举报
Androidsoftboard监听软键
前言:
我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面;在点击搜索框的时候,点击右下角的search符号键可以进行搜索;或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作。效果图:
function 1:
重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中监听KeyEventKey.KEYCODE_ENTER键(右下角确定键),当此键按下的时候,隐藏输入法软键盘,设置edittext内容和加载webview内容。[java] view
plaincopy
@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
plaincopy
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
plaincopy
edittext.setOnKeyListener(onKeyListener);
function 3:
第三种方法我认为可以帮助程序员更精确的判断右下角按键情况,以便应对更加复杂的情况。它可以帮助程序员依据当前邮件下为“GO”,“done”,“search”键的情况下做出更细分的操作。[java] view
plaincopy
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
plaincopy
<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相关文章推荐
- Cocos2dx 实现擦除 橡皮擦 刮奖 效果的实现
- android 整理了800篇非常经典的文章
- android手机可视化远程控制电脑的设计
- 小胖说事15--------iOS 应用内付费(IAP)开发步骤(最全)
- Android 5.0 版本使用ksoap2连接webservice报java.lang.ClassCastException
- 如何在android studio 1.0 启动时设置代理
- 解决导入android.support.v7的问题
- 关于android webview 端调原生app 的支付宝接口实现 还有BeeCloud(秒支付) 的接入使用
- Android控件之ScrollView(scrollbarStyle)
- Android访问远程数据库之调用Webservice
- 转载,自用,iOS中成员变量和属性的区别
- iOS 根据cell内容设置cell的高度
- iOS 关于音乐图片下载到本地沙盒的一些事
- 【Android】查看程序每个方法所花费的时间
- android 设置EditText光标位置
- Android优化
- IOS7设计指南
- Android 抽象布局include merge Viewstub
- quick cocos2d-x 实战:做一个手机横版格斗游戏4:游戏开始界面背景图代码
- 【搬运】Android四大基本组件介绍与生命周期