天天看点

Android监听输入法的状态:弹起,关闭Android监听输入法的状态:弹起,关闭

Android监听输入法的状态:弹起,关闭

  1. 在根视图添加一个id,

    @+id=root_view

    ,如
    Android监听输入法的状态:弹起,关闭Android监听输入法的状态:弹起,关闭
  2. 监听布局的状态
    @BindView(R.id.rl_rootview)
    FrameLayout frameLayout;
    
    frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (isShowBoard) {
                        int heightDiff = frameLayout.getRootView().getHeight() - frameLayout.getHeight();
                        if (heightDiff > dpToPx(getActivity(), 200)) { // if more than 200 dp, it's probably a keyboard...
                            // ... do something here
                            Log.d("----------", "打开");
     
                        } else {
                            Log.d("----------", "关闭");
                           
                        }
                    }
                }
            });
    
    
     //dp转px
        public float dpToPx(Context context, float valueInDp) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
        }
               
  3. manifest.xml

    中给activity设置

    windowSoftInputMode="adjustResize"

    <activity
        android:name=".activity.AddActivity"
        android:windowSoftInputMode="adjustResize"
        android:theme="@style/AppTheme.NoActionBar" />