Forcing User to Input Something in EditText in And

2019-07-11 19:46发布

问题:

I have several EditTexts in my Activty and I want my users to give inputs correctly before submitting the form. How can I do It? I have also spinners and RadioGroup Button.

回答1:

You can add Validation on submit button click:

private boolean validateFields() {
    int yourDesiredLength = 5;
    if (yourEditText1.getText().length() < yourDesiredLength) {
        yourEditText1.setError("Your Input is Invalid");           
        return false;
    }else if (yourEditText2.getText().length() < yourDesiredLength) {
        yourEditText1.setError("Your Input is Invalid");           
       return false;
    } 
    else{
        return true;
    }

On Submit button Click:

btnSubmit.setOnclickListener(new View.OnClickListener){
  @Override
        public void onClick(View v) {
            if(validateFields()){
                // Then Submit 
            }
        }
});

Output will be like this. Photo Credit.

Hope this helps.



回答2:

Apply validation for editText and all your other components in button.setOnClickListner

if all the validation satisfy then do further process.

like for edittext validation

 Send_button.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
if(edit.getText.equals(""))
{
  edit.setError("Please provide user name")
}
else
{
  //do your further activity
}
      }
                });


回答3:

Try this: code

I want to detect the Error before Submit Button is pressed

use setOnFocusChangeListener() on the Editext

 editText1 = (EditText) findViewById(R.id.editText1);

 editText1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
     String value = editText1.getText().toString();
        if(value.length() == 0) {  
            //editext is empty
            if(! hasFocus){
                editText1.requestFocus();
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                Toast.makeText(MainActivity.this, "Enter value correctly", Toast.LENGTH_SHORT).show();
            }
        }
   }
});


回答4:

I get your point, you can use TextInputLayout instead of EditText

define your xml layout:

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Password"/>
        </android.support.design.widget.TextInputLayout>

then you can use helper method inside your button click listener to validate the input data of all your editText

 public boolean validate() {
        boolean valid = true;
        if (password.isEmpty() || password.length() < 6) {
            _passwordText.setError("Password is too short !");
            valid = false;
        } else {
            _passwordText.setError(null);
        }

        return valid;
    }

don't forget to add the design support library dependency

compile 'com.android.support:design:25.1.1'

see this example: Creating a Login Screen Using TextInputLayout

it will be something like that if the user enter invalid data it will show this warning in red lines