安卓:禁用DialogFragment确定/取消按钮 [英] Android: disable DialogFragment OK/Cancel buttons
问题描述
我如何禁用确定/取消DialogFragment的按钮?
我试着打电话myAlertDialogFragment.getDialog(),但它总是返回null甚至一度显示片段
How can I disable OK/Cancel button of a DialogFragment when it is created using an AlertDialog ? I tried calling myAlertDialogFragment.getDialog() but it's always returning null even once the fragment is displayed
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();
}
}
我知道我可以膨胀的布局包含一个取消和OK键,但我宁愿使用如果可能的解决方案AlertDialog
I know I can to it by inflating a layout that contains both a cancel and an ok button, but I rather use the AlertDialog solution if possible
推荐答案
附上您的AlertDialog变量:
Attach your AlertDialog to variable:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
(initialization of your dialog)
AlertDialog alert = builder.create();
alert.show();
,然后从AlertDialogand获得按钮将它设置禁用/启用:
And then get button from your AlertDialogand set it disable/enable:
Button buttonNo = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonNo.setEnabled(false);
它给你机会,在运行时修改按钮属性。
It give you opportunity to change button properties on runtime.
然后回到您的警报变量。
Then return your alert variable.
AlertDialog必须获得其意见之前显示。
AlertDialog must be showed before acquiring its views.
这篇关于安卓:禁用DialogFragment确定/取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!