How to capture the “virtual keyboard show/hide” event in Android?

前端 未结 16 1197
暗喜
暗喜 2020-11-22 07:23

I would like to alter the layout based on whether the virtual keyboard is shown or not. I\'ve searched the API and various blogs but can\'t seem to find anything useful.

相关标签:
16条回答
  • 2020-11-22 08:02

    Based on the Code from Nebojsa Tomcic I've developed the following RelativeLayout-Subclass:

    import java.util.ArrayList;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.RelativeLayout;
    
    public class KeyboardDetectorRelativeLayout extends RelativeLayout {
    
        public interface IKeyboardChanged {
            void onKeyboardShown();
            void onKeyboardHidden();
        }
    
        private ArrayList<IKeyboardChanged> keyboardListener = new ArrayList<IKeyboardChanged>();
    
        public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public KeyboardDetectorRelativeLayout(Context context) {
            super(context);
        }
    
        public void addKeyboardStateChangedListener(IKeyboardChanged listener) {
            keyboardListener.add(listener);
        }
    
        public void removeKeyboardStateChangedListener(IKeyboardChanged listener) {
            keyboardListener.remove(listener);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
            final int actualHeight = getHeight();
    
            if (actualHeight > proposedheight) {
                notifyKeyboardShown();
            } else if (actualHeight < proposedheight) {
                notifyKeyboardHidden();
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        private void notifyKeyboardHidden() {
            for (IKeyboardChanged listener : keyboardListener) {
                listener.onKeyboardHidden();
            }
        }
    
        private void notifyKeyboardShown() {
            for (IKeyboardChanged listener : keyboardListener) {
                listener.onKeyboardShown();
            }
        }
    
    }
    

    This works quite fine... Mark, that this solution will just work when Soft Input Mode of your Activity is set to "WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE"

    0 讨论(0)
  • 2020-11-22 08:11

    I solve this by overriding onKeyPreIme(int keyCode, KeyEvent event) in my custom EditText.

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            //keyboard will be hidden
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:11

    Hide|Show events for keyboard can be listened through simple hack in OnGlobalLayoutListener :

     final View activityRootView = findViewById(R.id.top_root);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
    
                    if (heightDiff > 100) {
                        // keyboard is up
                    } else {
                        // keyboard is down
                    }
                }
            });
    

    Here activityRootView is your Activity's root view.

    0 讨论(0)
  • 2020-11-22 08:13

    I have solve the problem on single line textview back coding.

    package com.helpingdoc;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.LinearLayout;
    
    public class MainSearchLayout extends LinearLayout {
        int hieght = 0;
        public MainSearchLayout(Context context, AttributeSet attributeSet) {
    
            super(context, attributeSet);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.main, this);
    
    
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Log.d("Search Layout", "Handling Keyboard Window shown");
           if(getHeight()>hieght){
               hieght = getHeight();
           }
            final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
            final int actualHeight = getHeight();
            System.out.println("....hieght = "+ hieght);
            System.out.println("....actualhieght = "+ actualHeight);
            System.out.println("....proposedheight = "+ proposedheight);
            if (actualHeight > proposedheight){
                // Keyboard is shown
    
    
            } else if(actualHeight<proposedheight){
                // Keyboard is hidden
    
            }
    
            if(proposedheight == hieght){
                 // Keyboard is hidden
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:17

    This may not be the most effective solution. But this worked for me every time... I call this function where ever i need to listen to the softKeyboard.

    boolean isOpened = false;
    
    public void setListenerToRootView() {
        final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
    
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
                    Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboardup", 0).show();
    
                    if (isOpened == false) {
                        //Do two things, make the view top visible and the editText smaller
                    }
                    isOpened = true;
                } else if (isOpened == true) {
                    Toast.makeText(getApplicationContext(), "softkeyborad Down!!!", 0).show();
                    isOpened = false;
                }
            }
        });
    }
    

    Note: This approach will cause issues if the user uses a floating keyboard.

    0 讨论(0)
  • 2020-11-22 08:17

    I did this way:

    Add OnKeyboardVisibilityListener interface.

    public interface OnKeyboardVisibilityListener {
        void onVisibilityChanged(boolean visible);
    }
    

    HomeActivity.java:

    public class HomeActivity extends Activity implements OnKeyboardVisibilityListener {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        // Other stuff...
        setKeyboardVisibilityListener(this);
    }
    
    private void setKeyboardVisibilityListener(final OnKeyboardVisibilityListener onKeyboardVisibilityListener) {
        final View parentView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        parentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
            private boolean alreadyOpen;
            private final int defaultKeyboardHeightDP = 100;
            private final int EstimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
            private final Rect rect = new Rect();
    
            @Override
            public void onGlobalLayout() {
                int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, parentView.getResources().getDisplayMetrics());
                parentView.getWindowVisibleDisplayFrame(rect);
                int heightDiff = parentView.getRootView().getHeight() - (rect.bottom - rect.top);
                boolean isShown = heightDiff >= estimatedKeyboardHeight;
    
                if (isShown == alreadyOpen) {
                    Log.i("Keyboard state", "Ignoring global layout change...");
                    return;
                }
                alreadyOpen = isShown;
                onKeyboardVisibilityListener.onVisibilityChanged(isShown);
            }
        });
    }
    
    
    @Override
    public void onVisibilityChanged(boolean visible) {
        Toast.makeText(HomeActivity.this, visible ? "Keyboard is active" : "Keyboard is Inactive", Toast.LENGTH_SHORT).show();
      }
    }
    

    Hope this would help you.

    0 讨论(0)
提交回复
热议问题