我模仿了我认为相当标准的Dialog
代码:
public class DChooseSeparator extends DialogFragment
{
// ...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder
.setTitle("My Title")
.setView(myDialogLayout)
.setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
{
Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
}
else { myListener.onSet(myEditText.getText().toString()); }
}
})
.setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
return builder.create();
}
}
在展示它的onStart
的{{1}}中:
Fragment
但是,这不起作用,因为我的sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog();
myDialog.show(getFragmentManager(), "tMyDialogTag");
myDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false); // DOES NOT WORK
}
}
无法使用getButton
功能。我也不能在DialogFragment
课程中执行此操作,因为我需要首先DialogFragment
。
那么......在哪里可以/应该禁用show()
?我是否真的必须将Button
的整个创建移至Dialog
方法?
提前感谢您的帮助。
答案 0 :(得分:7)
您可以在创建Button
视图后启用或禁用FragmentDialog
。所以你必须在对话框的onStart()
方法中调用它。
请参阅我的代码:
public class DChooseSeparator extends DialogFragment
{
// MEMBER
private AlertDialog dialog;
private static boolean mEnableButton;
// You need an empty constructor: "All subclasses of Fragment must include a public empty constructor. "
// like it's described in the Fragment API -> so create a new Insatnce with this static methjod
public static DChooseSeparator newInstance(boolean enableButton){
mEnableButton = enableButton;
return new DChooseSeparator();
}
// ...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder
.setTitle("My Title")
.setView(myDialogLayout)
.setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
{
Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
}
else { myListener.onSet(myEditText.getText().toString()); }
}
})
.setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
dialog = builder.create()
return dialog;
}
@Override
public void onStart(){
super.onStart();
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(mEnableButton);
}
}
现在您可以像这样调用Dialog:
sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(false);
myDialog.show(getFragmentManager(), "tMyDialogTag");
}
}
答案 1 :(得分:0)
你需要在创建对话框的视图后,在dialogfragment类的oncreateview函数中调用它