关闭基于 editText 值的对话框
Dismiss a dialog based on editText value
我在 AlertDialog
中有一个 EditText
。只要在 EditText
中检测到单词 "stop",我希望关闭对话框。我尝试在对话框中调用 dismiss()
,但它不起作用:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog popup = builder.create();
final EditText edit = new EditText(this);
edit.setGravity(Gravity.CENTER);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence pRequest, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence pRequest, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String currentText = s.toString().toLowerCase();
if (currentText.contains("stop")) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); // Hide keyboard
popup.dismiss();
}
}
});
builder.setTitle("Value")
.setView(edit)
.show();
有什么办法解决这个问题吗?
对话框 popup
不是显示的对话框。您创建它,然后在调用 show()
时创建另一个完全不同的对话框。尝试直接在弹出窗口中调用命令:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog popup = builder.create();
final EditText edit = new EditText(this);
edit.setGravity(Gravity.CENTER);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence pRequest, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence pRequest, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String currentText = s.toString().toLowerCase();
if (currentText.contains("stop")) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); // Hide keyboard
popup.dismiss();
}
}
});
popup.setTitle("Value");
popup.setView(edit);
popup.show();
我在 AlertDialog
中有一个 EditText
。只要在 EditText
中检测到单词 "stop",我希望关闭对话框。我尝试在对话框中调用 dismiss()
,但它不起作用:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog popup = builder.create();
final EditText edit = new EditText(this);
edit.setGravity(Gravity.CENTER);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence pRequest, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence pRequest, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String currentText = s.toString().toLowerCase();
if (currentText.contains("stop")) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); // Hide keyboard
popup.dismiss();
}
}
});
builder.setTitle("Value")
.setView(edit)
.show();
有什么办法解决这个问题吗?
对话框 popup
不是显示的对话框。您创建它,然后在调用 show()
时创建另一个完全不同的对话框。尝试直接在弹出窗口中调用命令:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog popup = builder.create();
final EditText edit = new EditText(this);
edit.setGravity(Gravity.CENTER);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence pRequest, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence pRequest, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String currentText = s.toString().toLowerCase();
if (currentText.contains("stop")) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); // Hide keyboard
popup.dismiss();
}
}
});
popup.setTitle("Value");
popup.setView(edit);
popup.show();