![](/img/trans.png)
[英]How do I add an Alert Dialog to my Dice Game when a certain score is reached? Java, Android Studio
[英]How can I tell when my Android alert dialog is cancelled?
這是我的警報:
new AlertDialog.Builder(Activity.this)
.setMessage("You have unsaved text. Are you sure you want to leave?")
.setCancelable(true)
.setNegativeButton("Leave", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
finish();
}
})
.setPositiveButton("Stay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
請注意我如何允許警報取消: .setCancelable(true)
一旦用戶按下后退按鈕取消警報,我該如何運行一些代碼?
根據Android網站上的AlertDialog.Builder文檔,您可以使用setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)方法來處理取消對話框的時間。
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog)
{
// do what you need when the dialog is cancelled
}
})
所以,你的代碼將改為:
new AlertDialog.Builder(Activity.this)
.setMessage("You have unsaved text. Are you sure you want to leave?")
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog)
{
// do what you need when the dialog is cancelled
}
})
.setNegativeButton("Leave", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
finish();
}
})
.setPositiveButton("Stay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.