我有一个ALertDialog,允许用户输入引脚号。TextWatcher检查引脚的长度,然后在EditText中的文本长度与存储引脚的长度匹配时检查引脚是否正确。 。尝试成功进行销钉时,我试图关闭/取消对话框。 我已经尝试了dialog.dismiss();和dialog.cancel();但都不起作用。
为了清楚起见,我试图在if / else的“ else”部分中关闭AlertDialog。感谢您的关注
代码:
AlertDialog.Builder builder;
public void dialog() {
builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title");
// I'm using fragment here so I'm using getView() to provide ViewGroup
// but you can provide here any other instance of ViewGroup from your Fragment / Activity
View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.pword, (ViewGroup) getView(), false);
// Set up the input
final EditText input = (EditText) viewInflated.findViewById(R.id.input);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
builder.setView(viewInflated);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
stopReader();
startReader();
}
});
builder.show();
input.addTextChangedListener(new TextWatcher() {
@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) {
m_Text = input.getText().toString();
if (m_Text.length() == details.get(2).length()) {
if (!input.getText().toString().equals(details.get(2))) {
Toast.makeText(MainActivity.mainActivity, "Incorrect pin entered!", Toast.LENGTH_SHORT).show();
Log.d("eLOQ", "User pin is not correct");
input.setText("");
} else if (input.getText().toString().equals(details.get(2))) {
controller.startStageTwo();
Log.d("eLOQ", "User pin is correct");
dialog.dismiss();
}
}
}
});
}
答案 0 :(得分:1)
您必须在正确的位置使用 dialog.dismiss()。仅在“用户密码正确”时才关闭对话框,而在“密码尝试成功”时不关闭对话框。 如下更改代码:
@Override
public void afterTextChanged(Editable s) {
m_Text = input.getText().toString();
if (m_Text.length() == details.get(2).length()) {
if (!input.getText().toString().equals(details.get(2))) {
Toast.makeText(MainActivity.mainActivity, "Incorrect pin entered!", Toast.LENGTH_SHORT).show();
Log.d("eLOQ", "User pin is not correct");
input.setText("");
} else if (input.getText().toString().equals(details.get(2))) {
controller.startStageTwo();
Log.d("eLOQ", "User pin is correct");
}
dialog.dismiss(); // dismiss your dialog here. Here your dialog will dismiss in both caases
}
}
祝你好运:)
答案 1 :(得分:0)
dialog.hide(); or dialog.cancel();