這裏是你的問題的aswer:
//findViewById and bla bla bla...
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText1.setSelection(count); //set the cursor after the last character
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(editText1.length() == 1){ //check for number of characters
editText1.clearFocus();
editText2.requestFocus();
}
}
});
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText2.setSelection(count);
if(editText1.length() != 1){ //check if previous editText is empty
editText2.clearFocus();
editText1.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(editText2.length() == 1){
editText2.clearFocus();
editText3.requestFocus();
}
}
});
editText3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText3.setSelection(count);
if(editText1.length() != 1){ //check if first editText is empty
editText3.clearFocus();
editText1.requestFocus();
}
else if(editText2.length() != 1){ //check if second editText is empty
editText3.clearFocus();
editText2.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(editText3.length() == 1){
//Do something after the last editText was typed
}
}
});
//user pressed the back button
@Override
public void onBackPressed(){
if(editText2.isFocused()){
editText2.clearFocus();
editText1.requestFocus();
}
else if(editText3.isFocused(){
editText3.clearFocus();
editText2.requestFocus();
}
else super.onBackPressed();
}
節省你問的EditText之前清除焦點問另外一個問題,以更請求:) –