这是我的节目对话框代码。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Select Reminder which you want to delete")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
check = 1;
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
check = 2;
}
}
)
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(getBaseContext(), items[which] + (isChecked ? " checked!":" unchecked!") + which, Toast.LENGTH_SHORT).show();
}
}
).create();
}
return null;
}
这是我称之为的功能。
public void show()
{
showDialog(0);
if(check == 1)
{
Toast.makeText(this, "ok" + check, Toast.LENGTH_LONG).show();
}
else if (check == 2)
{
Toast.makeText(this, "Cancel" + check, Toast.LENGTH_LONG).show();
}
}
我面临有点问题,因为“showDialog(0);”所发生的事情感到困惑功能很好但是当我按下“确定”按钮然后对话框消失并且它只显示用“ok”按钮的onclicklistener写的吐司,但代码写在“showDialog(0);”之后显示另一个toast就像无法访问,意味着变量“check”(这是全局的),其值在“ok”和“cancel”按钮的onclicklisteners中设置为1或2,并在“showDialog”之后的if-else条件中使用它们);”显示不同的吐司但功能“显示”结束而不检查if-else条件。我不明白这里到底发生了什么?
答案 0 :(得分:0)
何时
调用 showDialog(0)
。然后执行此代码(在按下OK按钮之前)
if(check == 1)
{
Toast.makeText(this, "ok" + check, Toast.LENGTH_LONG).show();
}
else if (check == 2)
{
Toast.makeText(this, "Cancel" + check, Toast.LENGTH_LONG).show();
}
然后我猜check=0
,所以没有显示Toast
。
<强>更新强>
如果您想对用户执行某些操作,请按OK
,然后您需要执行此操作
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
check = 1;
//here do your work
}
}