Android: переходы EditText - PullRequest
       6

Android: переходы EditText

0 голосов
/ 28 августа 2018

У меня есть пользовательский ввод из 7 элементов управления EditText, как показано на рисунке ниже.

Я хочу, чтобы при вводе двух символов текста в один из этих EditText я автоматически переходил к следующему EditText. Как мне этого добиться?

enter image description here

Формат ввода:

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingVertical="@dimen/margin_small">

        <!-- AA -->
        <EditText 
            android:id="@+id/aaText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="AA"/>

        <!-- BB -->
        <EditText 
            android:id="@+id/bbText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="BB"/>

        <!-- CC -->
        <EditText 
            android:id="@+id/ccText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="CC"/>

        <!-- DD -->
        <EditText 
            android:id="@+id/ddText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="DD"/>

        <!-- EE -->
        <EditText 
            android:id="@+id/eeText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            style="@style/LocationEditTextStyle"
            android:imeOptions="actionNext"
            android:hint="EE"/>

        <!-- FF -->
        <EditText 
            android:id="@+id/ffText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="FF"/>

        <!-- GG -->
        <EditText 
            android:id="@+id/ggText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionDone"
            style="@style/LocationEditTextStyle"
            android:hint="GG"/>

    </LinearLayout>

Тип:

<style name="LocationEditTextStyle" parent="Widget.AppCompat.EditText">
        <item name="android:textAllCaps">true</item>
        <item name="android:inputType">textCapCharacters</item>
        <item name="android:maxLength">2</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:textSize">@dimen/location_text_size</item>
        <item name="android:theme">@style/LocationEditTextTheme</item>
    </style>

    <style name="LocationEditTextTheme" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/primary_text_disabled_material_light</item>
        <item name="colorControlActivated">@color/accentDarkColor</item>
    </style>

Я хочу, чтобы при вводе двух символов текста в один из этих EditText я автоматически переходил к следующему EditText. Как мне этого добиться?

1 Ответ

0 голосов
/ 28 августа 2018

Добавить TextWatcher

 editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.length() == 2){
                editText2.requestFocus();
            }
        }
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...