Android Constraint Layout onMeasure没有看到软键盘

时间:2017-08-29 10:49:51

标签: android android-softkeyboard android-constraintlayout

好的,所以我试图创建一个事件挂钩,以便在显示或隐藏软键盘时通知我。

我已将Constraint Layout分类如下:

public class MyConstraintLayout extends ConstraintLayout
{
    //public static final String KEYBOARD_STATE = "KEYBOARD_STATE", SHOWN = "SHOWN", HIDDEN = "HIDDEN";
    public static final String ACTION_KEYBOARD_SHOWN = "KEYBOARD_SHOWN";
    public static final String ACTION_KEYBOARD_HIDDEN = "KEYBOARD_HIDDEN";
    private LocalBroadcastManager localBroadcastManager;
    private Intent keyboardShown, keyboardHidden;
    private boolean shown = false;

    public MyConstraintLayout(Context context)
    {
        super(context);
        init(context);
    }

    public MyConstraintLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public MyConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        localBroadcastManager = LocalBroadcastManager.getInstance(context);
        keyboardShown = new Intent(ACTION_KEYBOARD_SHOWN);
        keyboardHidden = new Intent(ACTION_KEYBOARD_HIDDEN);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        //Log.d("Search Layout", "Handling Keyboard Window shown");
        final int proposedHeight = MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();

        Log.v("MyConstraintLayout", "onMeasure: actualHeight="+actualHeight+", proposedHeight="+proposedHeight);

        if(actualHeight > proposedHeight)
        {
            // Keyboard is shown
            if(!shown)
            {
                shown = true;
                localBroadcastManager.sendBroadcast(keyboardShown);
            }
        }
        else
        {
            // Keyboard is hidden
            if(shown)
            {
                shown = false;
                localBroadcastManager.sendBroadcast(keyboardHidden);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

但似乎布局根本没有看到键盘,因为当键盘显示为删除时,Log输出完全相同。 这是日志输出: 键盘显示时:

V/MyConstraintLayout: onMeasure: actualHeight=1120, proposedHeight=1280
V/MyConstraintLayout: onMeasure: actualHeight=1120, proposedHeight=1120

当键盘被隐藏时

V/MyConstraintLayout: onMeasure: actualHeight=1120, proposedHeight=1280
V/MyConstraintLayout: onMeasure: actualHeight=1120, proposedHeight=1120

我在运行Android 6.0.1的三星J500f上进行测试。

所以我真的有两个问题:
1 - 为什么布局没有看到键盘?
2 - 为什么在1120和1280之间使用proposedHeight flipfloping事件被触发两次?

另外需要注意的是,它被激活4次(仍然只是在相同的值之间进行翻转),直到我将android:windowSoftInputMode="adjustPan"添加到Manifest.xml。

0 个答案:

没有答案