自动移动到下一个编辑文本字段android

时间:2016-08-30 03:47:24

标签: android xml user-interface android-edittext addtextchangedlistener

This is image shows the UI I constructed我尝试构建验证码UI,用户可以输入通过短信收到的激活码。但是当它达到最大长度时,我无法移动到下一个编辑文本字段。这是我的xml代码。

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

   <TextView
       android:id="@+id/txtCode"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@+id/text_title"
       android:text="ENTER VERIFICATION CODE"
       android:gravity="center"
       android:layout_centerVertical="true"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:paddingTop="32dp"
       android:paddingBottom="16dp" />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_below="@id/txtCode"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearLayout">


    <EditText
        android:id="@+id/editvery1"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:background="@drawable/edittext_round"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:inputType="number"
        android:textColorHint="@android:color/holo_blue_dark"
        android:layout_marginRight="8dp"
        android:maxLength="1"
        android:nextFocusRight="@+id/editvery2"
        />
    <EditText
        android:id="@+id/editvery2"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:background="@drawable/edittext_round"
        android:inputType="number"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColorHint="@android:color/holo_blue_dark"
        android:layout_marginRight="8dp"
        android:nextFocusRight="@+id/editvery3"
        android:maxLength="1" />
    <EditText
        android:id="@+id/editvery3"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:background="@drawable/edittext_round"
        android:inputType="number"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColorHint="@android:color/holo_blue_dark"
        android:layout_marginRight="8dp"
        android:maxLength="1"
        android:nextFocusRight="@+id/editvery4"
        />

    <EditText
        android:id="@+id/editvery4"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:background="@drawable/edittext_round"
        android:inputType="number"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColorHint="@android:color/holo_blue_dark"
        android:maxLength="1"
        android:imeOptions="actionDone"
        />

           </LinearLayout> <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/linearLayout">
              <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/red_button"
                android:layout_below="@+id/linearLayout"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="44dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="VERIFY" android:onClick="Menu"
                android:textColor="@android:color/white"/>
              </ScrollView></RelativeLayout>

另外,我无法将滚动视图添加到相对布局的顶部。它会影响里面的所有内容。我尝试将textListener添加到我的java类中。似乎我坚持使用onTextChanged方法,因为它包含2个错误。

             import android.content.Intent;
             import android.support.v7.app.AppCompatActivity;
             import android.os.Bundle;
             import android.text.Editable;
             import android.text.TextWatcher;
             import android.view.View;
             import android.widget.EditText;

           public class VerifyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify);

    EditText et = (EditText) findViewById(R.id.editvery1);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {}

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        @Override
          public void onTextChanged(CharSequence s, int start, int before,          
                int count) {
            //if(s.length(1) != 0)
            // check your length here and change the focus to next edit text
         }
      });
  }}

1 个答案:

答案 0 :(得分:0)

使用TextChangedListener。试试这样的事情

Field1.addTextChangedListener(new TextWatcher() {

 @Override
  public void afterTextChanged(Editable s) {}

 @Override    
   public void beforeTextChanged(CharSequence s, int start,
   int count, int after) {
  }

@Override    
public void onTextChanged(CharSequence s, int start,
   int before, int count) {
   if(s.length() != 0)
    // ckeck your length here and chage the focus to next edite text
}
 });