问题
I have a program where i have an editText . I want to implement TextWatcher to reflect the changes in the total edittext. I want to show the alert dialog when editbox reach the max character limit 10. When i implement Text watcher it works but it show two alert box when it reach 10 character. here is my code
private TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// This sets a textview to the current length
// mTextView.setText(String.valueOf(s.length()));
}
public void afterTextChanged(Editable s) {
if (s.length() == 10) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder alert = new AlertDialog.Builder(
SendSms.this);
alert.setMessage("You Cross the limit of 10 Words !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
}
};
回答1:
This is common behavior of this method. You need to handle using flag.
Use this code
private TextWatcher mTextEditorWatcher = new TextWatcher() {
boolean flag = false;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() == 10) {
if (!flag) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder alert = new AlertDialog.Builder(
SendSms.this);
alert.setMessage("You Cross the limit of 10 Words !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
flag = true;
} else {
flag = false;
}
}
});
Other references
TextWatcher events are being fired multiple times
Events of TextWatcher are being called twice
Better is to use setError. Read how to use it.
回答2:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
SendSms.this);
AlertDialog alertDialog ;
private TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// This sets a textview to the current length
// mTextView.setText(String.valueOf(s.length()));
if (s.length() >= 10) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
alertDialogBuilder.setMessage("You Cross the limit of 10 Words !");
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alertDialog = alertDialogBuilder.create();
if(alertDialog != null && !alertDialog.isShowing()){
alertDialog.show();
}
}
else
{
mTextView.setText(String.valueOf(s.length()));
}
}
public void afterTextChanged(Editable s) {
}
};
I have editted the code . Globally declare alert Dialog and check whether alertDialog is already shown and then display the alert
回答3:
Try this..
have you tried in onTextChanged
private TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// This sets a textview to the current length
// mTextView.setText(String.valueOf(s.length()));
if (s.length() == 10) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder alert = new AlertDialog.Builder(
SendSms.this);
alert.setMessage("You Cross the limit of 10 Words !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
}
public void afterTextChanged(Editable s) {
}
};
回答4:
Simply make the alert instance global it will work fine.
private AlertDialog.Builder alert ;
in your onCreate
alert = new AlertDialog.Builder(
RegisterActivity.this);
in your afterTextChanged
public void afterTextChanged(Editable s) {
if (s.length() == 10) {
//messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
alert.setMessage("You Cross the limit of 10 Words !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
}
回答5:
Hi I did such work for phone number, when it reaches 5 digits, it automatically put space. You can utilize my code. Please check below code.
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.length() == 5 && start == 4) {
txtNumber.removeTextChangedListener(this);
txtNumber.append(" ");
txtNumber.addTextChangedListener(this);
}
if (s.length() == 6 && start == 5) {
txtNumber.removeTextChangedListener(this);
String newtext = s.toString().substring(0, s.length() - 1)
+ " " + s.charAt(s.length() - 1);
txtNumber.setText("");
txtNumber.append(newtext);
txtNumber.addTextChangedListener(this);
}
}
Let me know if you need more help
回答6:
Try Following Code :
Global Instance :
private AlertDialog.Builder alert;
TextWatcher :
private TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() >= 10) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
// alert = new AlertDialog.Builder(MainActivity.this);
// alert.setMessage("You Cross the limit of 10 Words !");
// alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int whichButton) {
// }
// });
// alert.show();
// Else you should use the following one to show error message
messageBody.setError("You Cross the limit of 10 Words !");
} else {
messageBody.setError(null);
}
}
public void afterTextChanged(Editable s) {
}
};
来源:https://stackoverflow.com/questions/20415856/text-watcher-in-android