Android软键盘状态弹出与消失的示例
最近遇到了关于软键盘的问题,需要获取到软键盘的状态,是否在显示,记录一下,方便以后查阅。网上常见的判定状态方法
getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
来判断软键盘是否打开,若相等则为打开。试了之后,发现这个只对手机自带的键盘有作用,对安装的第三方的输入法没有效果。
还有介绍使用InputMethodManager来获取键盘状态,代码如下
InputMethodManagerimm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); booleanisOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
这种并不能实时获取到键盘的状态,对我依然没有效果。
后来找到的解决方法,监听屏幕的变化,代码如下:
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Rect;
importandroid.os.Build;
importandroid.util.Log;
importandroid.util.TypedValue;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.ViewTreeObserver;
/**
*
*软键盘的监听
*/
publicclassKeyBoardShowListener{
privateContextctx;
publicKeyBoardShowListener(Contextctx){
this.ctx=ctx;
}
OnKeyboardVisibilityListenerkeyboardListener;
publicOnKeyboardVisibilityListenergetKeyboardListener(){
returnkeyboardListener;
}
publicinterfaceOnKeyboardVisibilityListener{
voidonVisibilityChanged(booleanvisible);
}
publicvoidsetKeyboardListener(finalOnKeyboardVisibilityListenerlistener,Activityactivity){
finalViewactivityRootView=((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener(){
privatebooleanwasOpened;
privatefinalintDefaultKeyboardDP=100;
//From@nathanielwolfanswer...Lollipopincludesbuttonbarintheroot.Addheightofbuttonbar(48dp)tomaxDiff
privatefinalintEstimatedKeyboardDP=DefaultKeyboardDP+(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP?48:0);
privatefinalRectr=newRect();
@Override
publicvoidonGlobalLayout(){
//Convertthedptopixels.
intestimatedKeyboardHeight=(int)TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP,EstimatedKeyboardDP,activityRootView.getResources().getDisplayMetrics());
//Concludewhetherthekeyboardisshownornot.
activityRootView.getWindowVisibleDisplayFrame(r);
intheightDiff=activityRootView.getRootView().getHeight()-(r.bottom-r.top);
booleanisShown=heightDiff>=estimatedKeyboardHeight;
if(isShown==wasOpened){
Log.e("Keyboardstate","Ignoringgloballayoutchange...");
return;
}
wasOpened=isShown;
listener.onVisibilityChanged(isShown);
}
});
}
}
用法如下:
//监听软键盘的状态
newKeyBoardShowListener(Activity.this).setKeyboardListener(
newKeyBoardShowListener.OnKeyboardVisibilityListener(){
@Override
publicvoidonVisibilityChanged(booleanvisible){
if(visible){
//软键盘已弹出
}else{
//软键盘未弹出
}
}
},Activity.this);
以下是可能会遇到的一些情况:
绑定软键盘到EditText
edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManagerinputManager=(InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit,0);
去除软键盘显示:
editMsgView.setText("");
editMsgView.clearFocus();
//closeInputMethodManager
InputMethodManagerimm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(),0);
始终不弹出软件键盘
EditTextedit=(EditText)findViewById(R.id.edit);edit.setInputType(InputType.TYPE_NULL);
也可以:
InputMethodManagerimm=(InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){//这里可以判断也可以不判断
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。