簡體   English   中英

EditText Android Studio

[英]EditText android studio

我在android studio上創建了一個應用程序,我有4個EditText來寫入IP地址,當我在EditText中寫入3個數字時,我想自動將光標移動到以下EditText。 怎么做謝謝你?

有多種方法可以做到這一點。

  1. 在每個這些EditText的xml中設置inputType和maxLength屬性。

    android:inputType =“ number” android:maxLength =“ 3”

接下來,定義下一個焦點項目:

android:nextFocusRight="id"
  1. 或者,您可以為每個EditText添加textChangedListener:

    editText1.addTextChangedListener(mTextWatcher);

    私人最終TextWatcher mTextWatcher = new TextWatcher(){

     @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (s.length() == 3) { // editText2.requestFocus(); } 

    };

嘗試此操作,您還需要向xml和每個視圖中添加兩個屬性:

android:focusable="true";
android:focusableInTouchMode="true"
You have to constantly check the input of each editText in 
onTextChanged method, To do this, you have to implement 
addTextChangedListener on each editText and check the charSequence if 
equal = 3.

for example

    yourFirstEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

     if(charSequence.toString().length() == 3) {
      //call next forcus
      yourSecondEditText.requestFocus();
           }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    yourSecondEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

     if(charSequence.toString().length() == 3) {
      //call next forcus
      yourThirdEditText.requestFocus();
           }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

//you don't need to do that for the last editText

 <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/AbsoluteLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="true" android:focusableInTouchMode="false" android:paddingEnd="3dp"> <Button android:id="@+id/IdOK" android:layout_width="118dp" android:layout_height="wrap_content" android:layout_x="123dp" android:layout_y="256dp" android:text="OK" /> <TextView android:layout_width="178dp" android:layout_height="36dp" android:text="Saisir l&apos;adresse IP" android:id="@+id/saisiAdIp" android:layout_x="96dp" android:layout_y="105dp" android:textSize="21dp" /> <EditText android:layout_width="60dp" android:layout_height="40dp" android:id="@+id/AdIpsaisi1" android:layout_x="35dp" android:layout_y="146dp" android:inputType="number" android:maxLength="3" android:focusable="true" android:focusableInTouchMode="true" android:nextFocusForward="@+id/AdIpsaisi2" /> <EditText android:layout_width="60dp" android:layout_height="40dp" android:id="@+id/AdIpsaisi2" android:layout_x="103dp" android:layout_y="146dp" android:inputType="number" android:maxLength="3" android:focusable="true" android:focusableInTouchMode="true" android:nextFocusRight="@+id/AdIpsaisi3"/> <EditText android:layout_width="60dp" android:layout_height="40dp" android:id="@+id/AdIpsaisi3" android:layout_x="170dp" android:layout_y="146dp" android:inputType="number" android:maxLength="3" android:focusable="true" android:focusableInTouchMode="true" android:nextFocusRight="@+id/AdIpsaisi4"/> <EditText android:layout_width="60dp" android:layout_height="40dp" android:id="@+id/AdIpsaisi4" android:layout_x="240dp" android:layout_y="146dp" android:inputType="number" android:maxLength="3" /> <TextView android:layout_width="10dp" android:layout_height="wrap_content" android:text="." android:id="@+id/textView" android:layout_x="96dp" android:layout_y="165dp" /> <TextView android:layout_width="10dp" android:layout_height="wrap_content" android:text="." android:id="@+id/textView6" android:layout_x="165dp" android:layout_y="165dp" /> <TextView android:layout_width="10dp" android:layout_height="wrap_content" android:text="." android:id="@+id/textView7" android:layout_x="231dp" android:layout_y="165dp" /> <TextView android:layout_width="184dp" android:layout_height="87dp" android:text="Adresse IP incorrect" android:id="@+id/erreursaisi" android:layout_x="116dp" android:layout_y="374dp" android:textSize="30dp" /> </AbsoluteLayout> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM