Android软键盘弹出与判断,Android
一:如何绑定软键盘到EditText:edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);
二:去除软键盘显示:editMsgView.setText("");
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
三:始终不弹出软件键盘EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);
也可以:InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){ //这里可以判断也可以不判断
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );
}
异常处理:软键盘不能弹出,最可能的原因是Android程序未将屏幕绘制完成,所以延迟一定时间,弹出软键盘。
处理方法一:private Handler hander=new Handler(){
publicvoid handleMessage(android.os.Message msg) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);
};
};
@Override
publicvoid onWindowFocusChanged(boolean hasWindowFocus) {
if(visible){
hander.sendEmptyMessageDelayed(0, 1000);
}
}
方法二:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
publicvoid run() {
InputMethodManager m = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 500);
是否开启状态判断:
方法一:// 虚拟键盘隐藏 判断view是否为空
View view = getActivity().getWindow().peekDecorView();
if (view != null) {
//隐藏虚拟键盘
InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(MainActivity.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);
}
方法二:relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);
relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {
public void onKeyboardStateChanged(int state) {
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏
//do sth
break;
case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示
//do sth
break;
default:
break;
}
}
});
android:id="@+id/keyboardRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true">
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
方法三:InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
更多:更改软键盘的状态,如显示则隐藏如隐藏则显示,取反ToggleInputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
版权声明:
作者信息(Author) :雪炭网 : 花花世界
原文链接(Hyperlink):https://snowcoal.com/article/641.html
原创内容,尊重版权,转载需注明出处;商业及其他特殊用途转载需原作者同意。