Android EditText, soft keyboard show/hide event?

前端 未结 7 1987
天涯浪人
天涯浪人 2020-11-29 03:43

Is it possible to catch the event that Soft Keyboard was shown or hidden for EditText?

7条回答
  •  一个人的身影
    2020-11-29 04:38

    In my case I wanted to hide a bottom bar when softkeyboard was shown. I considered best to just hide the bar when layout had less than a percent size of normal layout size. So I used this solution that works fine considering that soft keyboard usually takes 20% or more screen height. Just change the percent constant by any value you may think is ok. It needs attribute android:windowSoftInputMode="adjustResize" in manifest and layout must be the root to work.

    Extend from any layout you may want instead of RelativeLayout.

    public class SoftKeyboardLsnedRelativeLayout extends RelativeLayout {
        private boolean isKeyboardShown = false;
        private List lsners=new ArrayList();
        private float layoutMaxH = 0f; // max measured height is considered layout normal size
        private static final float DETECT_ON_SIZE_PERCENT = 0.8f;
    
        public SoftKeyboardLsnedRelativeLayout(Context context) {
            super(context);
        }
    
        public SoftKeyboardLsnedRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @SuppressLint("NewApi")
        public SoftKeyboardLsnedRelativeLayout(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int newH = MeasureSpec.getSize(heightMeasureSpec);
            if (newH > layoutMaxH) {
                layoutMaxH = newH;
            }
            if (layoutMaxH != 0f) {
                final float sizePercent = newH / layoutMaxH;
                if (!isKeyboardShown && sizePercent <= DETECT_ON_SIZE_PERCENT) {
                    isKeyboardShown = true;
                    for (final SoftKeyboardLsner lsner : lsners) {
                        lsner.onSoftKeyboardShow();
                    }
                } else if (isKeyboardShown && sizePercent > DETECT_ON_SIZE_PERCENT) {
                    isKeyboardShown = false;
                    for (final SoftKeyboardLsner lsner : lsners) {
                        lsner.onSoftKeyboardHide();
                    }
                }
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        public void addSoftKeyboardLsner(SoftKeyboardLsner lsner) {
            lsners.add(lsner);
        }
    
        public void removeSoftKeyboardLsner(SoftKeyboardLsner lsner) {
            lsners.remove(lsner);
        }
    
        // Callback
        public interface SoftKeyboardLsner {
            public void onSoftKeyboardShow();
            public void onSoftKeyboardHide();
        }
    }
    

    Example:

    layout/my_layout.xml

    
    
        ...
    
    

    MyActivity.java

    public class MyActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
            SoftKeyboardLsnedRelativeLayout layout = (SoftKeyboardLsnedRelativeLayout) findViewById(R.id.myLayout);
            layout.addSoftKeyboardLsner(new SoftKeyboardLsner() {
                @Override
                public void onSoftKeyboardShow() {
                    Log.d("SoftKeyboard", "Soft keyboard shown");
                }
    
                @Override
                public void onSoftKeyboardHide() {
                    Log.d("SoftKeyboard", "Soft keyboard hidden");
                }
            });
        }
    }
    

提交回复
热议问题