着眼于第二的EditText仅当第一非空机器人(focus on second edittext o

2019-07-18 02:53发布

目前我有两个EditText上,假设我想为空的EditText check.What验证是运行时验证更好的办法。

我的代码;

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

在这种的onclick第二EDITTEXT的它清除第二EditText上的焦点,当第一EDITTEXT是空的,但键盘输入总是在第二个文本做的,我们永远不能集中在第一个文本。

请不要对不同的EditText暗示不同的不同的焦点侦听器,因为这可能EDITTEXT动态地添加了。

更清楚:只需简单的事情失去焦点之前验证EV1,不允许任何之前字符在它进入其他视图获得焦点。

Answer 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);

         }
      }
    });


Answer 2:

我已在具有两个文本框的xml,并试图通过这种方式来解决问题。

检查下面的代码,告诉我是你合适吗?

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


Answer 3:

试试这个 :

编辑:

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

声明此方法:

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

谢谢。



文章来源: focus on second edittext only if first is non-empty android