1. 程式人生 > 其它 >實現EditText新增密碼輸入完成,自動跳到下一個輸入框

實現EditText新增密碼輸入完成,自動跳到下一個輸入框

技術標籤:personjavaandroidflutter

佈局檔案

<?xml version="1.0" encoding="utf-8"?>

<EditText  
    android:id="@+id/editText1"  
    android:layout_width="50dp"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:maxLength="10"  
    android:inputType="numberPassword" >  

    <requestFocus />  
</EditText>  

<EditText  
    android:id="@+id/editText2"  
    android:layout_width="50dp"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:maxLength="10"  
    android:inputType="numberPassword" />  

<EditText  
    android:id="@+id/editText3"  
    android:layout_width="50dp"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:maxLength="10"  
    android:inputType="numberPassword" />  

<EditText  
    android:id="@+id/editText4"  
    android:layout_width="50dp"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:maxLength="10"  
    android:inputType="numberPassword" />  

package com.chuanchuan.edittextdemo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.app.Activity;
public class MainActivity extends Activity {

private EditText et1;  
private EditText et2;  
private EditText et3;  
private EditText et4;  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
      
    et1=(EditText) findViewById(R.id.editText1);  
    et2=(EditText) findViewById(R.id.editText2);  
    et3=(EditText) findViewById(R.id.editText3);  
    et4=(EditText) findViewById(R.id.editText4);  
      
    final EditText[] mArray = new EditText[] { et1,et2, et3, et4 };  
      
    for (int i = 0; i < mArray.length; i++) {  
        final int j = i;  
        mArray[j].addTextChangedListener(new TextWatcher() {  
            private CharSequence temp;  
            private int sStart;  
            private int sEnd;  

            @Override  
            public void onTextChanged(CharSequence s, int start,  
                    int before, int count) {  
                temp = s;  
            }  

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

            @Override  
            public void afterTextChanged(Editable s) {  
                sStart = mArray[j].getSelectionStart();  
                sEnd = mArray[j].getSelectionEnd();  

                if (temp.length() == 3 && (j == 0 || j == 1 || j == 2)) {  
                    mArray[j + 1].setFocusable(true);  
                    mArray[j + 1].setFocusableInTouchMode(true);  
                    mArray[j + 1].requestFocus();  
                }  
                if (temp.length() > 3) {  

                    s.delete(sStart - 1, sEnd);  
                    int tSelection = sStart;  
                    mArray[j].setText(s);  
                    mArray[j].setSelection(tSelection);  

                }  
            }  
        });  
    }       
}  

}