Android:减少处理AlertDialog的代码
我对java开发相当陌生。我正试图用以下代码在Android应用程序中实现警报:Android:减少处理AlertDialog的代码,android,android-alertdialog,Android,Android Alertdialog,我对java开发相当陌生。我正试图用以下代码在Android应用程序中实现警报: AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setMessage("I'm a multi-button alert :-)"); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public
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();
它可以运行,但我想通过指向一个函数来避免每个按钮出现新的
DialogInterface.OnClickListener
。我认为这是可能的,但我不知道怎么做,有人能帮我吗?您可以为自己定义onClickListener并将其添加到按钮中,然后您必须在侦听器中确定调用了哪个按钮。这并不能真正减少代码量,但我更喜欢这种方式,因为可读性更好(?不确定这个词是否存在^^^)您可以在包含类中实现DialogInterface.OnClickListener,并监视要查看的参数,以及单击的按钮
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();
}
只需实现一个OnClickListener并设置它
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;
}
}
然后
alert.setPositiveButton("Ok", this);
alert.setNegativeButton("No", this);
alert.setNeutralButton("Cancel", this);
在AlertDialog.Builder上。方法稍有不同:
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);
已经有一些很好的答案,但我想我应该添加另一个变体:
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));
克里斯。。。我刚刚学到的一个技巧是,不需要在内部类中调用getBaseContext或getApplicationContext。您可以使用MyActivity.this,如下所示:
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//cancels itself?
/* Toast.makeText(ConfuseText.this,
"HELLO",
Toast.LENGTH_LONG)
.show();*/
}
我要相信你的话,你是Android开发的新手。首先,有三种方法来处理点击事件。匿名内部类(API 3),实现onClickListener(API 3)和XML属性(API 4)。IMHO内部类更“面向对象”,onClickListener可能更易于阅读
第二,考虑使用标准的Android架构来显示AlcutdiDube。此策略允许Android操作系统为您处理手机方向的更改。起初可能需要更多的工作,但以后会有回报。因此,您需要仔细阅读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;
}
第三,考虑尽可能保持事件处理程序尽可能接近ActReTalk代码,就像你正在做的那样。想象一下,如果您决定支持多个警报对话框。除非您有一个巨大的大脑,否则将方法和数据保持在“对象”附近就不太容易出错
是否有任何Android设计指南,说明在哪里定义onCreateDialog的id参数?有点像对话商店。另一方面,IMHO,让事件处理程序代码“靠近对象”是一个糟糕的设计指南,因为好的对象设计鼓励低耦合和高内聚。对我来说,正确的方法是创建一个处理程序控制器类并坚持MVC模式。
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();
}
});
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();