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

家住魔仙堡 提交于 2019-12-30 09:51:12

问题


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;

    final EditText ev1 = (EditText) findViewById(R.id.editText1);
    final EditText ev2 = (EditText) findViewById(R.id.editText2);


    ev1.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View rv, boolean hasFocus) {
    if(!hasFocus && ev1.getText().length()==0)
    {    
        ev1.requestFocus();
        ev2.clearFocus();
    }
    }
});

In this onclick of second editText it clears the focus of second EditText when First Edittext is empty,but keyboard entries always done in Second Text,and we can never get focus in First Text.

Please don't suggest different different focus listener for different EditText as this editText may be added dynamically too.

More CLEARLY:Just simple thing to validate ev1 before losing focus,not allow other views to get focus until any character is entered in it.


回答1:


    final EditText ev1 = (EditText) findViewById(R.id.editText1);
    final EditText ev2 = (EditText) findViewById(R.id.editText2);

    ev1.setOnFocusChangeListener(new OnFocusChangeListener() {      
        @Override
        public void onFocusChange(View rv, boolean hasFocus) {
            if(!hasFocus && ev1.getText().length()==0)
            {    
                  new Handler().postDelayed(new Runnable() {

                  @Override
                  public void run() {
                       ev2.clearFocus();
                   ev1.requestFocus();

                  }
            }, 100);

         }
      }
    });



回答2:


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();
            }
        }
    });



回答3:


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.



来源:https://stackoverflow.com/questions/14273190/focus-on-second-edittext-only-if-first-is-non-empty-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!