我试图在多次单击按钮时同时设置一个具有不同标题的对话框代码。我得到了输出,反复它取得了我宣布的第一个标题。
public class ButtonClickDialogueActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
static final int DIALOG_ID = 0;
public Button b1,b2,b3;
public String DiaTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.banana);
b2 = (Button)findViewById(R.id.milk);
b3 = (Button)findViewById(R.id.egg);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.banana:
DiaTitle="BANANA";
showDialog(DIALOG_ID);
break;
case R.id.milk:
DiaTitle="MILK";
showDialog(DIALOG_ID);
break;
case R.id.egg:
DiaTitle="EGG";
showDialog(DIALOG_ID);
break;
}
}
protected final Dialog onCreateDialog(final int id) {
Dialog dialog = null;
switch(id) {
case DIALOG_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(DiaTitle.toString());
builder.setMessage("Alert Box");
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "Banana Added into your Menu",Toast.LENGTH_SHORT).show();
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
dialog = alert;
break;
default:
}
return dialog;
}
}
答案 0 :(得分:0)
onCreateDialog()
仅被调用一次。您需要在onPrepareDialog()中设置标题,该标题在每次showDialog()
来电时运行。
简单摘要:
@Override
protected void onPrepareDialog(int id, Dialog dialog){
switch (id){
case DIALOG_ID:
dialog.setTitle(DiaTitle.toString());
break;
}
}
您可以保持现有代码的完整。
答案 1 :(得分:0)
我不知道如果我理解你的问题,但我认为你必须做些什么,
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.banana:
DiaTitle="BANANA";
ShowMessageT(DiaTitle, "Alert Box");
break;
case R.id.milk:
DiaTitle="MILK";
ShowMessageT(DiaTitle, "Alert Box");
break;
case R.id.egg:
DiaTitle="EGG";
ShowMessageT(DiaTitle, "Alert Box");
break;
}
public void ShowMessageT(final String title,final String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage("Alert Box");
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "Banana Added into your Menu",Toast.LENGTH_SHORT).show();
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
试试这个,让我知道发生了什么......