focus on second edittext only if first is non-empty android

后端 未结 3 707
别跟我提以往
别跟我提以往 2021-01-13 14:10

Currently I have two edit-text,suppose I want to make validation for empty edittext check.What is better way for runtime validation.

My code is;

             


        
3条回答
  •  暖寄归人
    2021-01-13 15:02

    Try this :

    EDIT :

            final EditText ev1,ev2,ev3,ev4,ev5,ev6;
    
            ev1 = (EditText) findViewById(R.id.editText1);
            ev2 = (EditText) findViewById(R.id.editText2);
            ev3 = (EditText) findViewById(R.id.editText3);
            ev4 = (EditText) findViewById(R.id.editText4);
            ev5 = (EditText) findViewById(R.id.editText5);
            ev6 = (EditText) findViewById(R.id.editText6);
    
            setValidateAction(ev2);
            setValidateAction(ev3);
            setValidateAction(ev4);
            setValidateAction(ev5);
            setValidateAction(ev6);
    

    Declare this method :

    public void setValidateAction(final EditText edit_action) {
            edit_action.setOnFocusChangeListener(new OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus && ev1.getText().length()==0){    
                        ev1.requestFocus();               
                    }
                }
            });
    }            
    

    Thanks.

提交回复
热议问题