gpt4 book ai didi

android - 在 DialogFragment 中禁用正/负按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:37 24 4
gpt4 key购买 nike

我模仿了我认为相当标准的 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();
}
}

在显示它的 FragmentonStart 中:

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 的整个创建移动到 onClick 方法吗?

预先感谢您的帮助。

最佳答案

您可以在创建FragmentDialog View 后启用或禁用Button。因此,您必须在 Dialog 的 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);
}
}

现在你可以这样调用你的对话框了:

sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(false);
myDialog.show(getFragmentManager(), "tMyDialogTag");
}
}

关于android - 在 DialogFragment 中禁用正/负按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21468326/

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