android 监听判断键盘弹出和收回

版权声明:转载时请标明出处。https://blog.csdn.net/mhhyoucom https://blog.csdn.net/mhhyoucom/article/details/81130837

在做公司项目时候发现要检查键盘弹出和收回来更新UI,那么本身android 键盘 api没有提供解决方案,那么只能够另辟蹊径。这里我使用的是检查view高度来实现键盘展开和收回监听,封装成一个类了:

代码:

public class CXKeyHelper implements View.OnLayoutChangeListener {

    private Activity activity;

    //Activity最外层的Layout视图
    private View activityRootView;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;

    private Runnable openRun;
    private Runnable closeRun;

    public void setOpenRun(Runnable openRun) {
        this.openRun = openRun;
    }

    public void setCloseRun(Runnable closeRun) {
        this.closeRun = closeRun;
    }

    public CXKeyHelper(Activity activity) {
        this.activity = activity;
    }

    public void pre() {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    public void init(ViewGroup viewGroup) {
        activityRootView = viewGroup;
        //获取屏幕高度
        screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight / 4;
    }

    public void 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 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
            if (openRun!=null)
                openRun.run();
        } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
            if (closeRun!=null)
                closeRun.run();
        }
    }
}

使用方法:

在onCreate中实现代码


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        keyHelper=new CXKeyHelper(this);
        keyHelper.pre();
        setContentView(R.layout.cx_more_img_activity);

        keyHelper.init(mainLayout);// mainLayout 根view
                keyHelper.setCloseRun(new Runnable() {
                    @Override
                    public void run() {
                        //收回回调
                    }
                });
                keyHelper.setOpenRun(new Runnable() {
                    @Override
                    public void run() {
                        //展开回调
                    }
                });


    }

在onResume 中实现

 @Override
    protected void onResume() {
        super.onResume();
        keyHelper.onResume();
    }

设置完毕,可以监听键盘打开和收回了。

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/81130837
今日推荐