其实我已经完成了这项功能,但我得到了最好的方法和最短的方式所以请帮助我。这是我的代码。我已经使用了其他方式,但发生了一些问题。如果还有另一个好方法,那么请帮助我。这对我来说是我项目中非常重要的一部分。提前谢谢。
package osvin.com.edittext;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity{
EditText edt_1,edt_2,edt_3,edt_4;
StringBuilder sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intialize();
edt_1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (sb.length() == 0 & edt_1.length() == 1) {
sb.append(s);
edt_1.clearFocus();
edt_2.requestFocus();
edt_2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (sb.length() == 1) {
sb.deleteCharAt(0);
Log.e("beforeTextChanged", "beforeTextChanged----------1");
}
}
public void afterTextChanged(Editable s) {
if (sb.length() == 0) {
edt_1.requestFocus();
Log.e("afterTextChanged", "afterTextChanged----------1");
}
}
});
edt_2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(sb.length()==0&edt_2.length()==1)
{
sb.append(s);
edt_2.clearFocus();
edt_3.requestFocus();
edt_3.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1){
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0){
edt_1.requestFocus();
}
}
});
edt_3.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(sb.length()==0&edt_3.length()==1){
sb.append(s);
edt_3.clearFocus();
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1){
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0){
edt_2.requestFocus();
}
}
});
edt_4.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(sb.length()==0&edt_4.length()==1){
sb.append(s);
//edt_3.clearFocus();
edt_4.requestFocus();
edt_4.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0){
edt_3.requestFocus();
}
}
});
}
public void Intialize(){
edt_1=(EditText)findViewById(R.id.edt_1);
edt_2=(EditText)findViewById(R.id.edt_2);
edt_3=(EditText)findViewById(R.id.edt_3);
edt_4=(EditText)findViewById(R.id.edt_4);
sb=new StringBuilder();
}
}