Android开发中,怎样监听键盘弹出收回事件( 二 )


想要监听键盘向下的事件,需要重写 Edittext.onKeyPreime() 方法,在这个方法里进行监听了.
我的博客有篇文章专门讲这个问题, 对Android 软键盘向下的监听

■网友
以前也有过类似的需求,在网上找了这么个方法,亲测有效,不过后来忘记由于什么原因没用这个,好像有点布局闪动,你可以试试。public class MainActivity extends Activity implements OnLayoutChangeListener{ //Activity最外层的Layout视图 private View activityRootView; //屏幕高度 private int screenHeight = 0; //软件盘弹起后所占高度阀值 private int keyHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activityRootView = findViewById(R.id.root_layout); //获取屏幕高度 screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); //阀值设置为屏幕高度的1/3 keyHeight = screenHeight/3; } @Override protected void onResume() { super.onResume(); //添加layout大小发生改变监听器 activityRootView.addOnLayoutChangeListener(this); } @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { //old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 // System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + oldBottom); // System.out.println(left + " " + top +" " + right + " " + bottom); //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if(oldBottom != 0 \u0026amp;\u0026amp; bottom != 0 \u0026amp;\u0026amp;(oldBottom - bottom \u0026gt; keyHeight)){ Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show(); }else if(oldBottom != 0 \u0026amp;\u0026amp; bottom != 0 \u0026amp;\u0026amp;(bottom - oldBottom \u0026gt; keyHeight)){ Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show(); } } 后来我自己用的是这种暴力的解决办法mEtMessage.setOnClickListener(new View.OnClickListener(){\t@Override\tpublic void onClick(View v){\t//键盘弹起事件 }});//可能是设置了进入的时候不允许获得焦点,第一次单击事件不会触发,所以还得加上这个mEtMessage.setOnFocusChangeListener(new View.OnFocusChangeListener(){\t@Override\tpublic void onFocusChange(View v, boolean hasFocus){\t\tif (hasFocus){\t\t//键盘弹起事件\t }\t}});}上面的方法看起来有点蠢,不过我后来也是实现了功能。有好的办法,欢迎留言。
■网友
监听布局的高度来判断软键盘的打开和关闭
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } private final List\u0026lt;SoftKeyboardStateListener\u0026gt; listeners = new LinkedList\u0026lt;SoftKeyboardStateListener\u0026gt;(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); //r will be populated with the coordinates of your view that area still visible. activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened \u0026amp;\u0026amp; heightDiff \u0026gt; 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened \u0026amp;\u0026amp; heightDiff \u0026lt; 100) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** * Default value is zero (0) * @return last saved keyboard height in px */ public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } }}使用:final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);softKeyboardStateHelper.addSoftKeyboardStateListener(...);// then just handle callbacks地址链接:http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android


推荐阅读