Как запретить раскладке автоматически открывать клавиатуру? - PullRequest
0 голосов
/ 07 августа 2020

Я использую MaterialChipsInput . У меня есть следующий код:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tile_divider2"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/header" />

    <com.pchmn.materialchips.ChipsInput
        android:id="@+id/chips1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:chip_backgroundColor="@color/primaryColor"
        app:chip_deletable="true"
        app:chip_hasAvatarIcon="false"
        app:chip_labelColor="@color/whiteColor"
        app:showChipDetailed="false"
        app:hint="@string/hint" />

</LinearLayout>

И код java:

        chipsInput = (ChipsInput) findViewById(R.id.chips1);
        chipsInput.addChipsListener(new ChipsInput.ChipsListener() {
            @Override
            public void onChipAdded(ChipInterface chip, int newSize) {}

            @Override
            public void onChipRemoved(ChipInterface chip, int newSize) {}

            @Override
            public void onTextChanged(CharSequence text) {
                if (text != null && text.toString().contains(" ") && text.toString().length() > 1) {
                    final String tag = StringUtils.capitalizeFully(text.toString().replaceAll(" ","").trim());
                    if (!(tag.isEmpty())) {
                        chipsInput.addChip(tag, null);
                    }
                }
            }
        });

По какой-то причине, когда я перехожу к действию, он загружает макет с этим компонентом , он автоматически открывает клавиатуру на ChipsInput. В чем может быть причина и как ее предотвратить?

Ответы [ 2 ]

0 голосов
/ 07 августа 2020

Поскольку ваш виджет (com.pchmn.materialchips.ChipsInput) является первым виджетом в Activity, этот виджет находится в фокусе.

Чтобы решить эту проблему, вам нужно отключить фокус, чтобы клавиатура не открывается при запуске активности

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

Проявление в теге текущей активности:

android:windowSoftInputMode="stateHidden"
0 голосов
/ 07 августа 2020

Вот несколько вещей, которые вы можете сделать, например:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Или

<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

Еще одно, что нужно попробовать

<style name="MyTheme" parent="Theme">
    <item name="android:windowSoftInputMode">stateHidden</item>
</style>

<application android:theme="@style/MyTheme">

Добавьте эти два свойства в родительский макет (например, линейный макет, относительный макет)

android:focusable="false"
android:focusableInTouchMode="false" 
...