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

前端 未结 16 1379
暗喜
暗喜 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 07:58

    Nebojsa Tomcic's answer wasn't helpful for me. I have RelativeLayout with TextView and AutoCompleteTextView inside it. I need to scroll the TextView to the bottom when the keyboard is showed and when it's hidden. To accomplish this I overrode onLayout method and it works fine for me.

    public class ExtendedLayout extends RelativeLayout
    {
        public ExtendedLayout(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 onLayout(boolean changed, int l, int t, int r, int b)
        {
            super.onLayout(changed, l, t, r, b);
    
            if (changed)
            {
                int scrollEnd = (textView.getLineCount() - textView.getHeight() /
                    textView.getLineHeight()) * textView.getLineHeight();
                textView.scrollTo(0, scrollEnd);
            }
        }
    }
    

提交回复
热议问题