如何在使用AlertDialog創建DialogFragment時禁用確定/取消按鈕? 我打過電話myAlertDialogFragment.getDialog(),但它總是返回null甚至一度片段顯示Android:禁用DialogFragment確定/取消按鈕
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解決方案,如果可能的
我試過了,但它不工作,因爲alert.getButton(AlertDialog.BUTTON_NEGATIVE);在alert.show()之前調用時會返回null null() 因此我不知道在哪裏調用它... – user1026605 2013-04-09 20:54:39
這樣做,是的(我個人覺得它真的很煩人)。你想要做的是在生命週期後面的某個地方做'setEnabled()'調用,可能在'onResume()'之後。 – Delyan 2013-04-09 20:55:44