简体   繁体   中英

EditText android studio

I create an application on android studio,I have 4 EditText to write an IP Address, I want to move automaticly the cursor to the following EditText when I write 3 numbers in an EditText. How to do Thanks You?

There are multiple ways to do this.

  1. Set the inputType and maxLength attributes in xml of each of these EditTexts.

    android:inputType="number" android:maxLength="3"

Next, define the next focus item:

android:nextFocusRight="id"
  1. Alternately you can add a textChangedListener for each of the EditText's:

    editText1.addTextChangedListener(mTextWatcher);

    private final 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(); } 

    };

Try this, also u need to add two attributes to the xml and each views as well:

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM