要在Android的DialogActivity中添加按鈕,可以通過以下步驟實現:
以下是一個簡單的示例代碼,演示如何在DialogActivity中添加按鈕:
public class CustomDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Dialog Title")
.setMessage("Dialog Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 按下OK按鈕時執行的操作
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 按下Cancel按鈕時執行的操作
dialog.dismiss();
}
});
return builder.create();
}
}
在創建DialogActivity時,可以使用FragmentManager來顯示DialogFragment:
CustomDialogFragment dialogFragment = new CustomDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "CustomDialogFragment");
通過以上步驟,可以在Android的DialogActivity中添加按鈕,并在按鈕點擊時執行相應的操作。