繁体   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