Android隐藏键盘在backkeypressed上

时间:2013-06-14 09:24:07

标签: android android-layout events

我已成功使用

制作了部分布局消息
    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);
            }
         }
    });

2 个答案:

答案 0 :(得分:2)

尝试这个

  @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);
    }

答案 1 :(得分:1)

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

@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);
}

来源:Any android event when keyboard slide out