在片段中显示/隐藏软键盘事件 [英] Show/Hide Soft Keyboard event in Fragment

查看:134
本文介绍了在片段中显示/隐藏软键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于查找显示/隐藏软键盘事件的文章很多. 我发现自己处于一种需要根据软键状态在一个片段中更改图标的情况下.

There are a lot of posts about finding a show/hide soft keyboard event. I find myself in a situation where I need to change an icon based on the soft key status, in a fragment.

我尝试实现onMeasure,但是无法在片段中覆盖它. 是否有(相对)轻松的方法来在片段中进行干净的显示/隐藏软键盘事件,还是我应该放弃运输?

I tried to implement onMeasure, but I can't override that in my fragment. Is there a (relative) painless way of getting a clean show/hide soft keyboard event in my fragment or should I abondon ship?

推荐答案

遗憾的是,事实如此-android在软件键盘显示事件上没有本机.

Sadly but true - android do not have native on software keyboard show event.

处理键盘隐藏问题的一种方法是检查输入的符号并按下后退按钮(例如textEdit甚至会收到后退按钮)-但这不是足够灵活的解决方案.

One the way handle fact that keyboard is hidden is to check entered symbols and back button press (for example textEdit will receive even back button) - but it is not flexible enough solution.

另一种可能的解决方案是: 在活动中覆盖onMeasure,然后通知观察者(模式观察者)-例如片段. Fragment应该订阅和取消订阅onPause onResume事件.诸如此类的活动代码:

Another the possible solutions is: Override onMeasure in activity and then notify observers (pattern Observer) - for example fragments. Fragment should subscribe and unsubscribe himself onPause onResume events. Something like that for activity code:

private class DialogActivityLayout extends LinearLayout {

        public DialogActivityLayout(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.activity_dialog, this);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec);
            final int actualHeight = getHeight();

            /* Layout loaded */
            if (actualHeight == 0 || proposedHeight == actualHeight) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                return;
            }

            if (proposedHeight > actualHeight) {
                DialogActivity.this.onKeyboardHide();
            } else {
                DialogActivity.this.onKeyboardShow();
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

我不确定,但据我所知,它仅适用于LinearLayout,当然,活动应设置adjustResize标志(以编程方式或在宣言中)

I'm not sure but as I remember it works only for LinearLayout and of course activity should have adjustResize flag set (programmatically or in manifeset)

另一种(我认为更好的方法)是使用globalTree观察器

Another (I thing better approach) is to as here with globalTree observer

SoftKeyboard在以下活动中打开和关闭监听器Android吗?

这篇关于在片段中显示/隐藏软键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆