繁体   English   中英

按键后的Android隐藏键盘

[英]Android hide keyboard on backkeypressed

我已经成功地使布局消失了

    activityRootView = findViewById(R.id.bottom_layout);    
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
           System.out.println("Height: "+heightDiff);

            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

            RelativeLayout bottom = (RelativeLayout)findViewById(R.id.bottom_layout);
            bottom.setVisibility(View.GONE);

            }else {

                RelativeLayout bottom = (RelativeLayout)findViewById(R.id.bottom_layout);
                bottom.setVisibility(View.VISIBLE);
            }
         }
    });

尝试这个

  @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
           button.setVisibility(View.VISIBLE);
          imageview.setVisibility(View.VISIBLE);
        InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }

还有另一种更合适的方法来检测键盘何时出现和隐藏:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
    } else {
        // Keyboard is hidden
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

来源: 键盘滑出时的任何android事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM