Forcing User to Input Something in EditText in And

2019-07-11 19:48发布

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.

4条回答
淡お忘
2楼-- · 2019-07-11 19:52

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
}
      }
                });
查看更多
倾城 Initia
3楼-- · 2019-07-11 19:53

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.

enter image description here

Hope this helps.

查看更多
贪生不怕死
4楼-- · 2019-07-11 19:57

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 enter image description here

查看更多
做自己的国王
5楼-- · 2019-07-11 20:08

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();
            }
        }
   }
});
查看更多
登录 后发表回答