0

This is image shows the UI I constructed我嘗試構建一個驗證代碼UI,用戶可以在其中輸入通過SMS收到的激活碼。但是當達到最大長度時,我無法移動到下一個編輯文本字段。這是我的xml代碼。自動移動到下一個編輯文本字段android

<?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 
     } 
     }); 
    }} 

回答

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 
} 
}); 
+0

@gpsrosak:迪迪,你得到的答案 – DKV

+0

我試圖用你的代碼,但我得到了這麼多的錯誤@V V – gpsrosak

相關問題