简体   繁体   中英

Android - Checking if editText is >3 before and after user input

I have some code where I need a "Create Account" button to be disabled if an editText field has less than 3 char in it. If the User enters 3 chars, then the button should enable itself so that it can be used.

I have constructed the if else statement that disables the button if there are less than 3 char in the editText field, BUT on input, when the user inserts 3 char, it does not re-evaluate to see if the statement is true so the button of course stays disabled.

Once the user enters 3 char into the edit text field, the button should enable itself.

    Button buttonGenerate = (Button) findViewById(R.id.btnOpenAccountCreate);
    userInitials = (EditText) findViewById(R.id.etUserChar);
    if (userInitials.getText().toString().length() > 3) {
                // Account Generator Button
                            buttonGenerate.setEnabled(true); // enable button;
                            buttonGenerate.setOnClickListener(new OnClickListener() { 
//Do cool stuff here
                                @Override
                                public void onClick(View v) {

                                }
                            });// END BUTTON
            } else {

                // If UserInitials is empty, disable button
                            Toast.makeText(this, "Please enter three(3) characters in the Initials Field ", Toast.LENGTH_LONG)
                                    .show();
                            buttonGenerate.setEnabled(false); // disable button;
            }// END IF ELSE

在此处输入图片说明

在此处输入图片说明

You want to use a TextWatcher

This will be triggered each time the text in your EditText which has this listener on it has changed. You just attach the listener to your EditText like you would any other listener then override its methods and , from the linked example below, check the length

 @Override
public void onTextChanged(CharSequence s, int start, int before, int count) 
{   
     if (s.length() > 2) 
     {
         buttonGenerate.setEnabled(true);
     }
     else
    {
         buttonGenerate.setEnabled(true);
    }
}

You don't need to check in your onClick() then, just disable the Button by default and enable in your onTextChanged() if the condition is met.

Rewritten

The above could be cleaned up as

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {   
     buttonGenerate.setEnabled((s.length() > 2));
}

I also have changed it to > 2 because I think that's actually what you want but the way you have it is a little confusing. You say "enter three(3)" which sounds like exactly 3 but your code looks different. Anyway, that's easy enough for you to change.

See this answer for an example

使用onTextChangedListener作为EditText来检查值并禁用或启用按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM