显示与隐藏
1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
3、调用隐藏系统默认的输入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)
4、获取输入法打开的状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
按键监听
1、重写Activity的dispatchKeyEvent(KeyEvent event)方法
//在其中监听KeyEventKey.KEYCODE_ENTER键(右下角确定键),当此键按下的时候,隐藏输入法软键盘,设置edittext内容和加载webview内容。
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);
}
2、使用OnKeyListener的方法来监听软键盘按键
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;
}
};
edittext.setOnKeyListener(onKeyListener);
3、使用OnEditorActionListener的方法来监听软键盘按键
这种方法我认为可以帮助程序员更精确的判断右下角按键情况,以便应对更加复杂的情况。它可以帮助程序员依据当前邮件下为“
GO
”,“done
”,“search
”键的情况下做出更细分的操作。
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键可以显示不同的文字和图案)。
<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,确定/完成,隐藏软键盘,即使不是最后一个文本输入框
引用:
Android 手动显示和隐藏软键盘 - 肖赛SoAi - 博客频道 - youkuaiyun.com
Android App监听软键盘按键的三种方式 - 祝福的博客 - 博客频道 - youkuaiyun.com