android表单验证如果条件为true,则Edittext一个接一个地聚焦

时间:2017-02-02 10:09:02

标签: android validation

如何验证表单firstname是否为true然后将下一个字段空转为下一个字段,如下一个字段为空。请帮我解决这个问题。

enter image description here

5 个答案:

答案 0 :(得分:1)

将testChangeListener用于该编辑文本字段。

        editText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            editText2.requestFocus();
        }
    });

答案 1 :(得分:1)

您可以为每个editText设置onClick侦听器。当单击edittext时,它将检查它的前一个字段是空还是有效

editTextLastName.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {
                          if(TextUtils.isEmpty(editTextFirstName.getText())){
                                 Toast.makeText(YourActivity.this, "Please fill up the first name", Toast.LENGTH_LONG).show();
                                 editTextFirstName.requestFocus();
                            }
                       }
                   });

答案 2 :(得分:0)

你可以通过添加这样的文本观察器来实现这一点,它只是一个基于你的要求的想法和格式

 etfirstname.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            String getPickupPlace = s.toString();
            String stringEditText = etfirstname.getText().toString().trim();

            if (etPickup.getText().toString().trim().length() == 0) {
                etfirstname.setClickable(false);
                etfirstname.setEnabled(false);
                etfirstname.setFocusable(false);

            } else {
                etfirstname.setClickable(true);
                etfirstname.setEnabled(true);
                etfirstname.setFocusable(true);
            }
        }
    });

答案 3 :(得分:0)

您可以使用edittext addTextChangedListener检查条件,如:

    editText1.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(<condition>){
             editText2.requestFocus();
        }
    }
});

为所有edittext提供该事件并设置导航流程。

答案 4 :(得分:0)

逐个确定你的每个字段(editText):

    editTextUsername = (EditText) findViewById(R.id.editText_enter_name);
    editTextmob= (EditText) findViewById(R.id.editText_mobNo);
    editTextEmail= (EditText) findViewById(R.id.editText_email);


        if (editTextUsername.getText().toString().trim().equals("")) {
            Toast.makeText(getApplicationContext(),
                    "Please enter username", Toast.LENGTH_SHORT)
                    .show();
        } else if (editTextmob.getText().toString().trim().equals("")) {
            Toast.makeText(getApplicationContext(),
                    "Please enter your mobile number", Toast.LENGTH_SHORT)
                    .show();
        } else if (editTextmob.getText().length() < 10) {
            Toast.makeText(getApplicationContext(),
                    "Please enter valid mobile number", Toast.LENGTH_SHORT)
                    .show();
        }
        else if (!editTextEmail.getText().toString().trim().equals(""))
        {
            if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
                    editTextEmail.getText().toString().trim()).matches()) {
                Toast.makeText(getApplicationContext(),
                        "Please enter valid e-mail id", Toast.LENGTH_SHORT)
                        .show();
            }
            else {

            //Do your stuff here
            }
        }