EditText获取焦点,Android软键盘遮挡Button,登录页面遇到的坑

  • ①先上效果图,如果不是你想要的效果,下边的可以不看了^^


    EditText获取焦点,Android软键盘遮挡Button,登录页面遇到的坑_第1张图片
    qq_pic_merged_1537369244838.jpg
  • ②下面是xml文件


    

        

           //此处是你编写的登录布局文件,正常写都可以,没什么特殊的


            
  • ③核心java代码
public void initView(){
 layout = findViewById(R.id.login_layout);
         //键盘显示,触摸键盘以外隐藏键盘
        findViewById(R.id.sv_content_ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                return false;
            }
        });
        layout.getViewTreeObserver().addOnGlobalLayoutListener(gLayoutListener);
}
 ViewTreeObserver.OnGlobalLayoutListener gLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            View focus = layout.findFocus();
            ScrollView sv = ((ScrollView) findViewById(R.id.scroll));
            sv.fullScroll(ScrollView.FOCUS_DOWN);

            if (focus != null && focus instanceof EditText) {//保证滑动之后 焦点依然未变
                focus.requestFocus();
            }
        }
    };

    @Override
    public void onDestroy() {

        layout.getViewTreeObserver().removeOnGlobalLayoutListener(gLayoutListener);
        gLayoutListener = null;
        super.onDestroy();
    }
  • ④AndroidMainfest.xml文件中Activity配置
 
  • ⑤ style.xml文件(手机状态栏颜色请根据你自己的设置)
 

  • ⑥end...如果你还没解决,一定仔细检查代码了

你可能感兴趣的:(EditText获取焦点,Android软键盘遮挡Button,登录页面遇到的坑)