我有一个ALertDialog,允许用户输入一个pin号码。。当文本的长度与文本的长度匹配时,检查pin的长度是否正确。我正在尝试关闭/取消此对话框,当pin尝试成功时。
我试过了对话框.关闭();以及对话框取消();但都没用。
为了清楚起见,我试图关闭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();
}
}
}
});
}