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

后端 未结 3 708
别跟我提以往
别跟我提以往 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 14:53

    I have taken on xml having two textboxes and tried this way to solve your issue.

    check the below code and tell me is it ok for you?

        setContentView(R.layout.activity_main);
        final EditText password = (EditText)findViewById(R.id.editText1);
        final EditText confirmpassword = (EditText)findViewById(R.id.editText2);
    
        password.addTextChangedListener(new TextWatcher() {
    
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(password.getText().toString().trim().length()==0)
                {
                    confirmpassword.setText("");
                }
            }
    
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }
    
            public void afterTextChanged(Editable s) {
            }
        });
        confirmpassword.setOnFocusChangeListener(new OnFocusChangeListener() {
    
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus && password.getText().toString().trim().length()==0)
                {
                    // First fill password
                    password.requestFocus();
                }
            }
        });
    

提交回复
热议问题