[英]android alert dialog not showing
我正在嘗試在onTouchListener內顯示警報,但無法顯示它。 我在這方面還很新,但是我一直在遵循一些很好的教程,但沒有用。 這是代碼的一部分...為什么不會顯示此警報?
mSwitcher.setOnTouchListener(new OnTouchListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
}
@Override public boolean onTouch(View v, MotionEvent event)
{
// the attempt at the alert
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return false;
}
});
我認為我的結構還可以,但是我什至無法編譯。
嘗試改變
AlertDialog alert = builder.create();
至
AlertDialog alert = builder.show();
我更新了代碼,以便將括號放在正確的位置。 它應該現在編譯。 正如JLund所指出的,更改builder.create();
的最后一行builder.create();
到builder.show();
那應該起作用。 如果您希望保留builder.create();
調用,只需添加alert.show();
之后。
你幾乎完成,以顯示你的對話框警告,但似乎你忘了你的顯示AlertDialog,使用show()
的AlertDialog
。
將此行添加到代碼末尾,但在return false;
之前return false;
在onTouch()
內部。
alert.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.