android 监听软键盘的显示与隐藏

来源:互联网 发布:学编程要多久 编辑:程序博客网 时间:2024/05/22 06:08
<span style="font-size:18px;">public class SoftInputObserver implements ViewTreeObserver.OnGlobalLayoutListener {    private View mRootView;    public SoftInputObserver(View rootView) {        mRootView = rootView.getRootView();        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);    }    @Override    public void onGlobalLayout() {        final int softKeyboardHeight = 100;        Rect r = new Rect();        mRootView.getWindowVisibleDisplayFrame(r);        DisplayMetrics dm = mRootView.getResources().getDisplayMetrics();        int heightDiff = mRootView.getBottom() - r.bottom;        if (heightDiff > softKeyboardHeight * dm.density){            Toast.makeText(mRootView.getContext(), "show", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(mRootView.getContext(), "hide", Toast.LENGTH_SHORT).show();        }    }}</span>

0 0
原创粉丝点击