尝试以下方法:
1) MnnActivity.class-----------
public class MnnActivity extends AppCompatActivity {
private EditText edt1;
private EditText edt2;
private EditText edt3;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
edt3 = (EditText) findViewById(R.id.edt3);
edt1.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(2)});
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length() > 1) {
edt2.requestFocus();
openKeyboard();
}
}
});
edt2.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(2)});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length() > 1) {
edt3.requestFocus();
openKeyboard();
}
}
});
edt3.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(4)});
edt3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(final Editable editable) {
if(editable.toString().length() > 3){
removeKeyboard();
}
}
});
}
private void openKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
private void removeKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
2) layout2.xml-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:weightSum="100"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="15"
android:text="P20"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt1"
android:layout_weight="15"
android:layout_gravity="center"
android:gravity="center"
android:background="@android:color/transparent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:layout_gravity="center"
android:gravity="center"
android:text="/"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt2"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="15"
android:background="@android:color/transparent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:layout_gravity="center"
android:gravity="center"
android:text="/"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt3"
android:layout_weight="30"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginEnd="5dp"
android:background="@android:color/transparent"/>
</LinearLayout>
3) 输出: