【发布时间】:2013-12-06 04:15:52
【问题描述】:
我有一个程序,其中有一个 editText 。我想实现 TextWatcher 以反映总编辑文本的变化。我想在编辑框达到最大字符限制 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();
}
}
};
【问题讨论】:
-
将
android:inputType="textNoSuggestions"添加到 EditText。这将起作用。无意义的解决方案,但有效 -
你应该使用 Toast 或
mTextEditorWatcher.showError("")而不是AlertDialog; -
在 EditText 的 XML 属性中,您可以添加:
android:maxLength="10" -
我和@SilentKiller 在一起,他是对的……更好的对话
-
@Gauravkumar 你想显示警报..??
标签: android android-edittext textwatcher