Identify which AlertDialog triggered onClick(DialogInterface dialog, int which)
I'm 开发者_开发技巧creating a dialog as follows:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_1:
return new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg1)
.setPositiveButton(android.R.string.ok, this)
.create();
case DIALOG_2:
...
...
}
return null;
}
@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (dialog == ???) {
...
}
else if (dialog == ???){
...
}
}
How do I identify which dialog triggered the onClick method? I can't declare the interface methods as in-line when creating the dialog because I want to access variables in my class. Every other interface passes some sort of id to its methods to identify which object called the method, but I can't seem to do anything with 'DialogInterface dialog'.
Perhaps you can extract the onclick listener as a seperate class and then pass in the dialog id? The interface is android.content.DialogInterface.OnClickListener
this is working for me
case Dialog_import_database:
return new AlertDialog.Builder(Main.this)
.setTitle(R.string.ImportDatabaseDialogTitle)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("Main","positive Button In dialog Box");
due what ever you wanna due when positive button is clicked here
}
})
.setNegativeButton("Cancel", null)
.setMessage(R.string.ImportDatabaseMessage)
.create();
}
This question is quite old and has been unanswered so far, so I've found a solution for this exact issue I've also run into.
Inside this activity I've launched there's the possibility, depending on the user's actions, to fire 2 distinct custom DialogFragment
s. Let's call them DialogFragment
A
or B
. We create these with the standard builder and then .show(fragmentManager, <TAG>)
where tag could either be A_TAG
or B_TAG
to easily identify them later.
So either one of these DialogFragment
is shown, you will want to decide what to do in your implemented onClick(DialogInterface dialog, int buttonClicked)
and here's how I've done so:
final DialogFragment aDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(A_TAG);
final DialogFragment bDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(B_TAG);
if (aDialogInterface != null) {
//do something for dialogfragment A
} else if (bDialogInterface != null) {
//do something for dialogfragment B
}
basically doing a null-check on findFragmentByTag(...)
...
I have met with the same issue. And right now I have found following solution.
private Dialog[] aaa = new Dialog[2];
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_1:
aaa[0] = new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg1)
.setPositiveButton(android.R.string.ok, this)
.create();
retrun aaa[0];
case DIALOG_2:
...
...
}
return null;
}
@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (dialog == aaa[0])) {
...
}
else if (dialog == aaa[1]){
...
}
}
This is an old question and still I've encountered the same problem xO. As a way around I've used the "hashCode" of the Dialog object. Depending on the dialog type you can cast the "Dialog Interface" to your specific type, and compare the "hashCode" of the "first" and "second" dialog vs the "hasCode" of the "DilaogInterface" object from the callback function.
"public void onClick(DialogInterface dialog, int whichButton) {}"
Function signature.
Is hard two identify directly which dialog has been showed instead not which button has been pressed, so if you populate the dialogs with different buttons you can do the trick. Something like this:
new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg1)
.setPositiveButton(android.R.string.ok, this)
.create();
new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg2)
.setNegativeButton(android.R.string.ok, this)
.create();
@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (whichbutton == DialogInterface.BUTTON_POSITIVE) {
...
}
else if (whichButton == DialogInterface.BUTTON_NEGATIVE){
...
}
精彩评论