我有一个程序,我有一个editText。我想实现TextWatcher以反映总edittext中的更改。我想在editbox达到最大字符限制10时显示警告对话框。当我实现文本观察器时,它可以工作,但是当它达到10个字符时它会显示两个警告框。这是我的代码
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();
}
}
};
答案 0 :(得分:1)
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) {
}
};
我编写了代码。全局声明警报对话框并检查是否已显示alertDialog,然后显示警报
答案 1 :(得分:1)
这是此方法的常见行为。你需要处理使用旗帜。
使用此代码
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;
}
}
});
其他参考资料
TextWatcher events are being fired multiple times
Events of TextWatcher are being called twice
最好是使用setError。阅读how to use it.
答案 2 :(得分:0)
试试这个..
你试过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) {
}
};
答案 3 :(得分:0)
只需将警报实例设为全局,即可正常使用。
private AlertDialog.Builder alert ;
你的onCreate 中的
alert = new AlertDialog.Builder(
RegisterActivity.this);
你的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();
}
}
答案 4 :(得分:0)
您好我为电话号码做了这样的工作,当它达到5位数时,它会自动放置空间。你可以利用我的代码。请查看以下代码。
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);
}
}
如果您需要更多帮助,请告诉我
答案 5 :(得分:0)
尝试以下代码:
全球实例:
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) {
}
};