我有两个执行不同活动的Dialog。
我的查询是否可以使用单个onclicklistener来执行不同的功能?
例如:
private OnClickListener onOkClickListener = new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if ( first dialog's ok button clicked)
{
/* some actions*/
}
else if ( second dialog's ok button clicked)
{
/* some other actions*/
}
}
};
有人请帮我做这个......
答案 0 :(得分:1)
对于这些对话框和片段以及所有...
,可以使用两个或更多按钮的单个侦听器我已经通过一些教程找到了它。这是答案。
通过将tagName设置为那些特定的对话框或片段,我们可以区分上下文并可以重用相同的侦听器 示例代码:
dialog.show(getFragmentManager(), fragment1_tag);
dialog.show(getFragmentManager(), fragment2_tag);
以下是唯一一个将被标记名称重用的侦听器。
View.onClickListener okButtonListener(new View.onClickListener{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if( view.getTag().equals("fragment1_tag") )
{
//Do operation
}
else if( view.getTag().equals("fragment2_tag") )
{
//Do operations
}
}
});
答案 1 :(得分:0)
是肯定的。您可以使用。考虑这个例子......
final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog alertDialog = (AlertDialog) dialog;
String s = (String) alertDialog.getButton(
DialogInterface.BUTTON_POSITIVE).getTag();
boolean isDialog1 = s.equals("DIALOG1");
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Negative in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Negative in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
case DialogInterface.BUTTON_NEUTRAL:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Neutral in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Neutral in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
case DialogInterface.BUTTON_POSITIVE:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Positive in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Positive in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
};
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SecondActivity.this);
builder.setTitle("Dialog1 Title");
builder.setMessage("Dialog1 Message");
builder.setPositiveButton("Yes", clickListener);
builder.setNegativeButton("No", clickListener);
builder.setNeutralButton("Cancel", clickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTag(
"DIALOG1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SecondActivity.this);
builder.setTitle("Dialog2 Title");
builder.setMessage("Dialog2 Message");
builder.setPositiveButton("Yes", clickListener);
builder.setNegativeButton("No", clickListener);
builder.setNeutralButton("Cancel", clickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTag(
"DIALOG2");
}
});