最近遇到了关于软键盘的问题,需要获取到软键盘的状态,是否在显示 ,记录一下,方便以后查阅。网上常见的判定状态方法
复制代码 代码如下:
getwindow().getAttributes().softinputMode== WindowManager.LayoutParams.soFT_input_STATE_UnspecIFIED
来判断软键盘是否打开,若相等则为打开。试了之后,发现这个只对手机自带的键盘有作用,对安装的第三方的输入法没有效果。
还有介绍使用inputMethodManager 来获取键盘状态,代码如下
inputMethodManager imm = (inputMethodManager)getSystemService(Context.input_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
这种并不能实时获取到键盘的状态,对我依然没有效果。
后来找到的解决方法,监听屏幕的变化,代码如下:
import androID.app.Activity;import androID.content.Context;import androID.graphics.Rect;import androID.os.Build;import androID.util.Log;import androID.util.TypedValue;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEwTreeObserver;/** * * 软键盘的监听 */public class KeyBoardShowListener { private Context ctx; public KeyBoardShowListener(Context ctx) { this.ctx = ctx; } OnKeyboardVisibilityListener keyboardListener; public OnKeyboardVisibilityListener getKeyboardListener() { return keyboardListener; } public interface OnKeyboardVisibilityListener { voID onVisibilityChanged(boolean visible); } public voID setKeyboardListener(final OnKeyboardVisibilityListener Listener,Activity activity) { final VIEw activityRootVIEw = ((VIEwGroup) activity.findVIEwByID(androID.R.ID.content)).getChildAt(0); activityRootVIEw.getVIEwTreeObserver().addOnGlobalLayoutListener(new VIEwTreeObserver.OnGlobalLayoutListener() { private boolean wasOpened; private final int DefaultKeyboardDP = 100; // From @nathanIElwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP ? 48 : 0); private final Rect r = new Rect(); @OverrIDe public voID onGlobalLayout() { // Convert the dp to pixels. int estimatedKeyboardHeight = (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP,EstimatedKeyboardDP,activityRootVIEw.getResources().getdisplayMetrics()); // Conclude whether the keyboard is shown or not. activityRootVIEw.getwindowVisibledisplayFrame(r); int heightDiff = activityRootVIEw.getRootVIEw().getHeight() - (r.bottom - r.top); boolean isShown = heightDiff >= estimatedKeyboardHeight; if (isShown == wasOpened) { Log.e("Keyboard state","Ignoring global layout change..."); return; } wasOpened = isShown; Listener.onVisibilityChanged(isShown); } }); }}
用法如下:
//监听软键盘的状态new KeyBoardShowListener(Activity.this).setKeyboardListener( new KeyBoardShowListener.OnKeyboardVisibilityListener() { @OverrIDe public voID onVisibilityChanged(boolean visible) { if (visible) { //软键盘已d出 } else { //软键盘未d出 } } },Activity.this);
以下是可能会遇到的一些情况:
绑定软键盘到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 inputMethodManagerinputMethodManager imm = (inputMethodManager)getSystemService(Context.input_METHOD_SERVICE);imm.hIDeSoftinputFromWindow(editMsgVIEw.getwindowToken(),0);
始终不d出软件键盘
复制代码 代码如下:
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软键盘状态d出与消失的示例全部内容,希望文章能够帮你解决Android软键盘状态d出与消失的示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)