我正在将我的 Android 应用程序转换为使用 fragment 。以前,我有一个现在是 fragment 的 Activity 。因此,不能再使用此代码:
showDialog(CONFIRM_ID);
// ...
@Override
public Dialog onCreateDialog(int id) {
// Create the confirmation dialog...
}
在 Fragment
对象中,我需要显示一个确认对话框,确认后我会返回对象以进行状态更新。
例如
- 内部 fragment X。
- 显示确认对话框。
- 如果"is"更新 X 的 UI。
我怎样才能做到这一点?请提供工作示例代码。
最佳答案
您可以使用此处显示的代码简单地创建对话框 (AlertDialog):http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
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();
alert.show();
如果您需要使用不会立即关闭对话框的按钮创建对话框,您可以在此处查看我的答案:How to prevent a dialog from closing when a button is clicked
关于android - 在 Fragment 中显示确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762525/