软键盘的显示和隐藏

来源:互联网 发布:navicat for mysql语法 编辑:程序博客网 时间:2024/06/13 02:31

在android开发的过程中,有的时候我们需要使软键盘在点击edittext时显示出来,而在点击其他地方的时候消失,并且在软键盘弹出之后,整个页面变得可以滑动。比较常见的像登陆界面。
这个功能其实不难实现。代码如下

给整个页面设置触摸事件,imm.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), 0); 就是让软键盘消失。

ll_preall.setOnTouchListener(new OnTouchListener() {            @Override        public boolean onTouch(View arg0, MotionEvent arg1) {            switch (arg1.getAction()) {            case MotionEvent.ACTION_DOWN:                dy = arg1.getY();                break;            case MotionEvent.ACTION_UP:                uy = arg1.getY();                if((uy-dy)<=10||(uy-dy)>=-10){                    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);                    return imm.hideSoftInputFromWindow(getCurrentFocus()                            .getWindowToken(), 0);                  }                break;            default:                break;            }            return true;        }        });        这个部分是点击返回键时键盘也消失。findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() {        @Override        public void onClick(View arg0) {            InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);            inputMethodManager.hideSoftInputFromWindow(QingTieXinXiActivity.this.getCurrentFocus().getWindowToken(),            InputMethodManager.HIDE_NOT_ALWAYS);            QingTieXinXiActivity.this.finish();        }    });
0 0
原创粉丝点击