我有一个工作的 DialogFragment,它使用内部类在某些对象上做一些事情,设置菜单图标等。当我去 Android Studio 时,我意识到这是不正确的,我一直在尝试改变内部类是静态的。
在这样做的过程中,我现在正尝试使用 onCreateDialog,根据 Google docs 、“doPositiveClick”和“doNegativeClick”,以便调用 MainActivity 可以在这些对象上完成工作,而不是由 fragment 执行。
然而,现在让我感到困惑的是如何在 fragment 中设置布局 - 我可以输入标题、消息和按钮:
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.alert_title)
.setMessage(R.string.alert_message)
.setPositiveButton(R.string.set,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doNegativeClick();
}
}
)
但之前我是这样布局的:
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
input.setText("5");
input.setLayoutParams(lp);
input.setRawInputType(Configuration.KEYBOARD_QWERTY);
问题是,它在 onCreateDialog 中的什么位置? Google 文档展示了如何在对话框 textView 上设置文本,但那是在 onCreateView() 中。
我的困惑是谷歌文档没有同时执行这两项操作,即它没有显示如何同时设置自定义元素,以及如何在调用 MainActivity 中设置正/负点击 - 或者如果确实如此,我很抱歉我现在看不到它。
因此,任何人都可以使用上面的 onCreateDialog 让我更清楚,我怎样才能拥有一个 editText 字段,其默认值接受用户输入,然后将该输入返回给 doPositiveClick() 进行处理。
最佳答案
DialogFragment 有两种使用方式:对话框或 View 。
case1:使用DialogFragment
作为对话框。您必须实现 onCreateDialog()
才能返回对话框。然后必须以下列方式显示对话框。看例子:
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();
}
按以下方式创建和显示对话框。以这种方式显示不关心,是否实现了onCreateView()
。
// Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");
case2:用作 View (它不是对话框的功能)。这只是观点。您必须按以下方式实现 onCreateView()
并显示对话框:
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("This is an instance of MyDialogFragment");
return v;
}
并且必须显示如下 View 。与使用 Fragment 类相同。以这种方式显示不关心,是否实现了onCreateDialog()
。
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
ft.add(R.id.embedded, newFragment);
ft.commit();
总结:
在设计中,您可以同时实现 onCreateView()
和 onCreateDialog()
,并在 DialogFragment
生命周期中使用相同的源代码。如果屏幕小,使用DialogFragment
作为Dialog。如果屏幕很大,请使用 DialogFragment
作为 View (相同的通用 Fragment 类)。
注意使用正确的方式显示DialogFragment
以适应onCreateView()
和onCreateDialog()
以防止异常。
关于android - DialogFragment 中 onCreateView 和 onCreateDialog 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27544122/