How to change the focus to next edit text in android?

后端 未结 9 1672
南笙
南笙 2020-12-03 01:03

\"enter

The User can enter only one digit in the edit text. if he enters the value in

9条回答
  •  温柔的废话
    2020-12-03 01:41

    set android:maxLength="1" to all your ExitText in xml

    Try the following code

    edtxt1 = (EditText) findViewById(R.id.edtxt_phonenumber_one);
            edtxt2 = (EditText) findViewById(R.id.edtxt_phonenumber_two);
            edtxt3 = (EditText) findViewById(R.id.edtxt_phonenumber_three);
    
            edtxt1.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
    
                    if (s.length() ==1) {
                        edtxt2.requestFocus();
                    }
    
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                }
            });
    
            edtxt2.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    if (s.length() == 1) {
                        edtxt3.requestFocus();
                    }
    
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
    
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
    
                }
            });
            edtxt3.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    if (s.length() == 1) {
                        edtxt1.requestFocus();
                    }
    
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
    
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
    
                }
            });
    

    This should work

提交回复
热议问题