Reputation: 3708
I've successfully displayed an EditText
in the AlertDialog
. When entered text in the edit field, it is compared with a custom text. If the entered text doesn't match, the AlertDilaog
should continue to show but currently the dialog is closing Positive Button is clicked, even if a wrong password was entered.
Do you guys have a solution for this?
UPDATED: Here is the code
builder.setView(v)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText enteredPassword = (EditText) v.findViewById(R.id.enteredPassword);
if (enteredPassword.getText().toString().trim().equals(correctPassword.trim()) {
Log.i(TAG, "User Entered Right Answer");
} else {
Log.i(TAG, "User Entered Wrong Answer");
// Continue showing the dialog if Wrong Answer is entered
Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Upvotes: 0
Views: 1062
Reputation: 614
Yes, you can. You basically need to:
So, create a listener class:
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If tou want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
Then when showing the dialog use:
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
Remember, you need to show the dialog otherwise the button will not be findeable. Also, be sure to change DialogInterface.BUTTON_POSITIVE
to whatever value you used to add the button. Also note that when adding the buttons in the DialogBuilder
you will need to provide onClickListeners
- you cannot add the custom listener in there, though - the dialog will still dismiss if you do not override the listeners after show()
is called.
Upvotes: 1
Reputation: 18112
Change your code as Part 1 and use Part 2 just after you show the dialog
Part 1
builder.setView(v)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Don't do anything here. Will override it later
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Part 2
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
EditText enteredPassword = (EditText) v.findViewById(R.id.enteredPassword);
if (enteredPassword.getText().toString().trim().equals(correctPassword.getText().toString().trim()) {
Log.i(TAG, "User Entered Right Answer");
disalog.dismiss();
} else {
Log.i(TAG, "User Entered Wrong Answer");
// Continue showing the dialog if Wrong Answer is entered
Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
}
}
});
Upvotes: 0
Reputation: 75788
You can use dialog.dismiss();
in your condition .
FYI
Use dialog.dismiss(); instead dialog.cancel();
Finally, Like this way
if (enteredPassword.getText().toString().trim().equals(correctPassword.getText().toString().trim()) {
Log.i(TAG, "User Entered Right Answer");
dialog.dismiss();
} else {
Log.i(TAG, "User Entered Wrong Answer");
// Continue showing the dialog if Wrong Answer is entered
Toast.makeText(getApplicationContext(), "Enter a proper answer", Toast.LENGTH_LONG).show();
}
Please have a look
http://developer.android.com/reference/android/app/Dialog.html#dismiss()
Upvotes: 1
Reputation: 372
Try this...
dialogBuilder.setPositiveButton(R.string.ok, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(is text correct)
{
dialog.dismiss();
}
}
});
Upvotes: 0