gpt4 book ai didi

Android:禁用 DialogFragment 确定/取消按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:31 25 4
gpt4 key购买 nike

当使用 AlertDialog 创建 DialogFragment 时,如何禁用它的确定/取消按钮?我尝试调用 myAlertDialogFragment.getDialog() 但它总是返回 null,即使在显示 fragment 后也是如此

public static class MyAlertDialogFragment extends DialogFragment {

public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");

return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}

我知道我可以通过膨胀包含取消和确定按钮的布局来实现它,但如果可能的话我宁愿使用 AlertDialog 解决方案

最佳答案

您需要覆盖 DialogFragment 中的 onStart() 并保留对按钮的引用。然后您可以使用该引用稍后重新启用该按钮:

Button positiveButton;

@Override
public void onStart() {
super.onStart();
AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
}

}

关于Android:禁用 DialogFragment 确定/取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15912124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com