Android метод onDraw () не вызывается в первый раз - PullRequest
1 голос
/ 09 мая 2019

Я протестировал эту клавиатуру 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...