`

键盘相关

阅读更多

虽然很简单,而且没有什么价值,但还是记录一下方便自己查找使用:

 

InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

 

这两行代码起的作用是:当键盘当前激活状态时,会隐藏键盘,隐藏状态时,会激活键盘。

 

 

如何监听键盘的改变,目前并没有发现好的方法,有一个方法倒是可以起到一些作用:原理是监听根视图的变化,当根视图高度变化超过一定的值的时候,就认为键盘弹出了:

 

 final RelativeLayout rlRootView = (RelativeLayout)findViewById(R.id.rl_rootview);//这个是你的根视图
        rlRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = rlRootView.getRootView().getHeight() - rlRootView.getHeight();
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                	
                	Log.e("*******heightDiff:", ""+heightDiff);
                }
             }
        });

 

  注意,需要在使用到键盘的Activity中添加:

android:windowSoftInputMode="adjustResize"

还有就是当前的Activity不能为全屏,即不能有这个属性,否则监听不到变化

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

 

 自动弹出键盘:

android:windowSoftInputMode="stateAlwaysVisible"

如果要让底部的状态栏显示在键盘之上(即,被键盘推上去)的话,需要加上adjustResize。

android:windowSoftInputMode="adjustResize|stateAlwaysVisible"

 

 

参考链接:

http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics