第一次不调用android onDraw()方法

时间:2019-05-09 12:59:52

标签: java android android-softkeyboard

我已经测试过this的android键盘,但是我检测到一个问题,当我调试源代码时,第一次检测到键盘中的方法onDraw()时,键盘没有显示。文件KeyboardView.java不会在第一次被调用,即当我退出该应用程序并返回时,将显示键盘,之后将为所有应用程序显示。

更新:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Round up a little
    if (mKeyboard == null) {
        setMeasuredDimension(getPaddingLeft() + getPaddingRight(), getPaddingTop() + getPaddingBottom());
    } else {
        int width = mKeyboard.getMinWidth() + getPaddingLeft() + getPaddingRight();
        //MeasureSpec.getSize(widthMeasureSpec) = 0 when is called at first time (and in this case onDraw will not be called),
        //after that when I quit my application and reopen it the value became different of 0, onDraw will be called and keyboard is displayed
        if (MeasureSpec.getSize(widthMeasureSpec) < width + 10) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        setMeasuredDimension(width, mKeyboard.getHeight() + getPaddingTop() + getPaddingBottom());
    }
}

我说过MeasureSpec.getSize(widthMeasureSpec)第一次调用时等于0。 我认为这是第一次通话时不显示键盘的根本原因。

有人可以解释为什么MeasureSpec.getSize(widthMeasureSpec)返回0吗?

这是我的布局:

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/ime_background_letters" >
    <FrameLayout
            android:id="@+id/keyboard_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|bottom" >
        <com.android.inputmethod.latin.car.KeyboardView
                android:id="@+id/keyboard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="true"
                app:keyTextColorPrimary="@color/ime_foreground_numbers"
                android:layout_gravity="center_horizontal"
                style="@style/Keyboard" />
        <com.android.inputmethod.latin.car.KeyboardView
                android:id="@+id/popup_keyboard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/keyboard_popup_offset"
                android:visibility="gone"
                app:keyTextColorPrimary="@color/ime_foreground_numbers"
                android:layout_gravity="top|center"
                style="@style/Keyboard" />
    </FrameLayout>
    <FrameLayout
            android:id="@+id/lockout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.935"
            android:clickable="true" >
        <TextView
                android:id="@+id/lockout_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:layout_gravity="center"
                android:textColor="@color/ime_foreground_letters"
                android:textSize="@dimen/keyboard_lockout_text_size"
                android:text="@string/park_to_use_keyboard" />
    </FrameLayout>
</FrameLayout>

0 个答案:

没有答案