Android软键盘的打开和关闭的监控
最近客户出了一个很变态的需求:当客户在输入框输入内容后,关闭软键盘的时候刷新数据。我去,旁边加个Button按钮就很轻松的问题,搞这么复杂。没思路,找度娘寻思路:
准照度娘的开发经验,搞了一个Demo,可以用了,具体如下:
Demo的xml文件:
Activity代码:
public class InputDialogActivity extends BaseActivity {@BindView(R.id.etListener)EditText mEtListener;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_input_dialog);ButterKnife.bind(this);setListenerFotEditText(findViewById(R.id.activity_main_layout));}private void setListenerFotEditText(View view){SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(view);softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {@Overridepublic void onSoftKeyboardOpened(int keyboardHeightInPx) {//键盘打开Toast.makeText(InputDialogActivity.this, mEtListener.getText().toString() + "打开", Toast.LENGTH_SHORT).show();}@Overridepublic void onSoftKeyboardClosed() {//键盘关闭Toast.makeText(InputDialogActivity.this, mEtListener.getText().toString() + "关闭", Toast.LENGTH_SHORT).show();}});}
}
SoftKeyboardStateHelper帮助类文件如下:
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {public interface SoftKeyboardStateListener {void onSoftKeyboardOpened(int keyboardHeightInPx);void onSoftKeyboardClosed();}private final List listeners = new LinkedList();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);}@Overridepublic 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 && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...isSoftKeyboardOpened = true;notifyOnSoftKeyboardOpened(heightDiff);} else if (isSoftKeyboardOpened && heightDiff < 100) {isSoftKeyboardOpened = false;notifyOnSoftKeyboardClosed();}}public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {listeners.add(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();}}}
}
经过整合和解耦,使用起来很简单,直接调用一个方法即可,效果如下:
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
