键盘弹起和消失的监听2中方式

第一种方式:该方式适用于键盘弹起,然后activity被顶上去的情况,因为只有activity被顶上去了,才能计算高度差。如果不能顶上去,那么高度差就为0,所以监听不到键盘的弹起和消失。下面请看代码:

第一步 activity 配置  android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

第二步 activity 实现 implements OnLayoutChangeListener

第三步 

 public class MainActivity extends Activity implements OnLayoutChangeListener{  
      
    //Activity最外层的Layout视图  
    private View activityRootView;  
    //屏幕高度  
    private int screenHeight = 0;  
    //软件盘弹起后所占高度阀值  
    private int keyHeight = 0;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        activityRootView = findViewById( R.id.root_layout);  
        //获取屏幕高度  
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();  
        //阀值设置为屏幕高度的1/3  
        keyHeight = screenHeight/3;  
    }  
      
    @Override  
    protected void onResume() {  
        super.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)){  
              
            Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();  
          
        }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){  
              
            Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();  
          
        }  
          
    }  

第二种方式:该方式适用于键盘弹起,activity并没有被顶上去的情况,我觉得这种方式应该也是适用于键盘把activity顶起来的情况,下面看代码:

int KeyStatus;
int STATUS_HIDE = 0;
int STATUS_SHOW = 1;

protected void onResume() {
    super.onResume();
    getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            Rect rect = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            if (bottom != 0 && oldBottom != 0 && bottom - rect.bottom < ScreenUtils.getScreenHeight(mContext)/4 && KeyStatus != STATUS_HIDE) {
                Log.i("LOG_CAT", "键盘消失");
                KeyStatus = STATUS_HIDE;
            } else if (bottom != 0 && oldBottom != 0 && bottom - rect.bottom > /*tempheight*/ScreenUtils.getScreenHeight(mContext)/4 && KeyStatus != STATUS_SHOW) {
                Log.i("LOG_CAT", "弹出键盘");
                KeyStatus = STATUS_SHOW;
            }
        }
    });

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值