Domanda

How to make a Toast to pop up when the user tries to insert text longer than maximum limit?

<EditText
    android:maxLength="28"
    ...
 />

I tried to use TextWatcher but it doesn't work properly:

public class MyActivity extends Activity implements TextWatcher
{

    // ...

    @Override
    public void afterTextChanged(Editable arg0){}

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
    {
        if(etUsername.isFocused() && etUsername.getText().length() == 28)
            Toast.makeText(MyActivity.this, "The username must be at most 28 characters!" , Toast.LENGTH_SHORT).show();
        else if(etPassword.isFocused() && etPassword.getText().length() == 10)
            Toast.makeText(MyActivity.this, "The password must be at most 10 characters!" , Toast.LENGTH_SHORT).show();
    }
}

EDIT:

I also tried to put the code inside afterTextChanged and beforeTextChanged but it doesn't solve the problem.

EDIT2:

What I want is that the Toast only can be popped up when there are 28 characters in the EditText and the user is trying to add a 29th character. The Toast in my code above will pop up only if there are 27 characters in the TextEdit and the user inserts a 28th character.

È stato utile?

Soluzione

The only way to achieve desired functionality is to change android:maxLength to desiredMaxLength + 1 and to check for the length in afterTextChanged(). You will have to manually delete the last character when length == desiredMaxLength + 1 (i.e. manipulate the s parameter in afterTextChanged(Editable s)). Please be careful not to put yourself in infinite loop because after changing s afterTextChanged() will be called again.

Altri suggerimenti

This solution work for my case:

    @Override
    public void afterTextChanged(Editable s) {
        if(etUsername.isFocused() && etUsername.getText().toString().trim().length() > MAX_CHARS){
        etUsername.setText(s.toString().substring(0, MAX_CHARS));
        etUsername.setSelection(s.length()-1);
            Toast.makeText(getActivity(), "Maximum number of characters reached.", Toast.LENGTH_SHORT).show();
        }
    }

Hope it helps.

Instead of:

if(etUsername.isFocused() && etUsername.getText().length() == 28)

Use this:

 if(arg0.length() > 28)
      {
String temp=arg0.subString(0,27)
etUsername.setText(temp);
 //Toast come's here
       }

Complete code is below. it is working perfectly.

String tempReview;
EditText et_userReview;
TextView tv_charactersRemaining;

Public void afterTextChanged(Editable s){

if(s.toString().length() <= 200) {
                 tv_charactersRemaining.setText(200 -   s.toString().length()+ " characters remaining");
                 tempReview = s.toString();
                 } else {
                 et_userReview.setText(tempReview);
                 Editable etext = et_userReview.getText();
                 Selection.setSelection(etext, 200);    
Toast.makeText(getApplicationContext(),  "No more characters You have reached the limit.", Toast.LENGTH_SHORT).show();
                 }

}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top