Reputation: 12194
I'm rather new to java development. I'm trying to implement Alert in an Android app with the following code:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("I'm a multi-button alert :-)");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "KO", Toast.LENGTH_LONG).show();
}
});
alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_LONG).show();
}
});
alert.show();
It runs but I would like to avoid, for every button, the new DialogInterface.OnClickListener
by pointing to a single function on which handling clicked button. I think this is possible but I don't know how, can anyone help me?
Upvotes: 3
Views: 8130
Reputation: 1849
builder = new AlertDialog.Builder(this);
ListAdapter adapter = new ActionAdapter(this,
R.layout.spinner_row_comman, arraylist_date, builder);
builder.setTitle("Select days to set Reminder");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (which == 0) {
} else if (which == 1) {
dayDate = 2;
Log.e("dayDate", "++++++++++++++++" + dayDate);
updateLabel();
} else if (which == 2) {
} else if (which == 3) {
} else if (which == 4) {
} else if (which == 5) {
}
}
});
builder.show();
Upvotes: 0
Reputation: 53
d.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
d.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
d.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_LONG).show();
}
});
Upvotes: 0
Reputation: 3319
Chris... A TIP I just learned is that you do not need to call getBaseContext or getApplicationContext in an inner class. You can use MyActivity.this as in:
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//cancels itself?
/* Toast.makeText(ConfuseText.this,
"HELLO",
Toast.LENGTH_LONG)
.show();*/
}
I am going to take your word that you are new to Android development. So first, there are three ways to handle click events. Anonymous inner classes (API 3), implementing onClickListener (API 3) and XML attributes (API 4). IMHO inner classes are more "object oriented" and onClickListener may be easier to read.
Second, consider using the standard Android architecture to display AlertDialogs. This strategy allows the Android OS to handle phone orientation changes for you. It may be more work initially, but it will pay off later. So you would need to read up on onCreateDialog.
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_ABOUT:
// do the work to define the About Dialog
dialog= getInstanceAlertDialog();
break;
default:
dialog = null;
break;
}
return dialog;
}
private AlertDialog getInstanceAlertDialog() {
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setMessage("Confuse Text JALComputing Copyright 2011");
AlertDialog alert= builder.create();
alert.setTitle("About");
return alert;
}
Third, consider keeping the event handlers as close to the AlertDialog code as possible, JUST AS YOU ARE DOING. Just imagine if you decide to support multiple alert dialogs. Unless you have a giant brain, keeping the methods and data close to the "object" is much less prone to error
Upvotes: 1
Reputation: 10908
There are some good answers already, but I thought I'd add another variation:
public class MyClickListener implements DialogInterface.OnClickListener {
String mDisplayText;
Context mCtx;
public MyClickListener(String displayText, Context ctx){
mDisplayText = displayText;
mCtx = ctx;
}
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mCtx,
mDisplayText,
Toast.LENGTH_LONG)
.show();
}
}
...
AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setMessage("I'm a multi-button alert :-)");
alert.setPositiveButton("Ok", new MyClickListener("OK",this));
alert.setNegativeButton("No", new MyClickListener("KO",this));
alert.setNeutralButton("Cancel", new MyClickListener("CANCEL",this));
Upvotes: 1
Reputation: 4494
Slightly different approach:
final Context context = getApplicationContext(); // Should be "final", won't compile otherwise
class MyListener implements DialogInterface.OnClickListener {
MyListener() {
}
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,
which == DialogInterface.BUTTON_POSITIVE ? "OK" :
which == DialogInterface.BUTTON_NEGATIVE ? "KO" :
"CANCEL",
Toast.LENGTH_LONG)
.show();
}
}
MyListener listener = new MyListener();
AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setMessage("I'm a multi-button alert :-)");
alert.setPositiveButton("Ok", listener);
alert.setPositiveButton("No", listener);
alert.setPositiveButton("Cancel", listener);
Upvotes: 2
Reputation: 73484
Just implement an OnClickListener and set it.
class MyActivity implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(getApplicationContext(), "OK",Toast.LENGTH_LONG).show();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(getApplicationContext(), "KO",Toast.LENGTH_LONG).show();
break;
case DialogInterface.BUTTON_NEUTRAL:
Toast.makeText(getApplicationContext(), "CANCEL",Toast.LENGTH_LONG).show();
break;
}
}
and then
alert.setPositiveButton("Ok", this);
alert.setNegativeButton("No", this);
alert.setNeutralButton("Cancel", this);
on the AlertDialog.Builder.
Upvotes: 1
Reputation: 104168
You can implement the DialogInterface.OnClickListener in the containing class and monitor the which
parameter to see, which button was clicked.
alert.setPositiveButton("Ok", this);
alert.setNegativeButton("No", this);
alert.setNeutralButton("Cancel", this);
public void onClick(DialogInterface dialog, int which) {
String text = "";
switch (which)
{
case DialogInterface.BUTTON_NEGATIVE:
text = "Cancel";
}
Toast.makeText(getApplicationContext(),
text,
Toast.LENGTH_LONG)
.show();
}
Upvotes: 11
Reputation: 11227
you are able to define the onClickListener for its own and add it to the buttons, then you have to determine in the Listener which Button was calling. This do not really reduce the amount of code, but I prefer this way because of better readability (? not sure if this word exists ^^)
Upvotes: 1