谷歌建议我们使用DialogFragment而不是使用Fragments API创建一个简单的对话框,但是使用一个孤立的DialogFragment来创建一个简单的Yes-No确认消息框是荒谬的。在这种情况下,最佳实践是什么?
当前回答
我建议使用DialogFragment。
当然,用它创建一个“Yes/No”对话框是相当复杂的,因为它应该是相当简单的任务,但用dialog创建一个类似的对话框也是令人惊讶地复杂。
(活动的生命周期使它变得复杂-你必须让Activity管理对话框的生命周期-没有办法传递自定义参数,例如自定义消息给Activity。如果使用API级别低于8)
这样做的好处是,你通常可以很容易地在DialogFragment之上构建自己的抽象。
其他回答
DialogFragment具有对话框和片段的功能。基本上所有的生命周期事件都是由DialogFragment自动管理的,比如屏幕配置的改变等。
对话框:对话框是一个小窗口,提示用户做出决定或输入额外的信息。
DialogFragment:一个DialogFragment是一个特殊的片段子类,它被设计用来创建和托管对话框。它允许FragmentManager管理对话框的状态,并在发生配置更改时自动恢复对话框。
在AlertDialog上使用DialogFragment:
Since the introduction of API level 13: the showDialog method from Activity is deprecated. Invoking a dialog elsewhere in code is not advisable since you will have to manage the the dialog yourself (e.g. orientation change). Difference DialogFragment - AlertDialog Are they so much different? From Android reference regarding DialogFragment: A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Other notes Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes. DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.
是的,使用DialogFragment和onCreateDialog,你可以简单地使用AlertDialog构建器创建一个简单的AlertDialog与Yes/No确认按钮。根本没有多少代码。
关于处理事件在你的片段会有各种方法,但我只是在我的片段定义了一个消息处理程序,通过它的构造函数传递到DialogFragment,然后将消息传递回我的片段的处理程序适当的各种点击事件。有很多方法,但下面的方法对我来说是有效的。
在对话框中保存一个消息并在构造函数中实例化它:
private Message okMessage;
...
okMessage = handler.obtainMessage(MY_MSG_WHAT, MY_MSG_OK);
在你的对话框中实现onClickListener,然后适当地调用处理程序:
public void onClick(.....
if (which == DialogInterface.BUTTON_POSITIVE) {
final Message toSend = Message.obtain(okMessage);
toSend.sendToTarget();
}
}
Edit
由于Message是可打包的,你可以在onSaveInstanceState中保存它并恢复它
outState.putParcelable("okMessage", okMessage);
然后在onCreate中
if (savedInstanceState != null) {
okMessage = savedInstanceState.getParcelable("okMessage");
}
我可以建议简化一下@ashishduh的回答吗:
public class AlertDialogFragment extends DialogFragment {
public static final String ARG_TITLE = "AlertDialog.Title";
public static final String ARG_MESSAGE = "AlertDialog.Message";
public static void showAlert(String title, String message, Fragment targetFragment) {
DialogFragment dialog = new AlertDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
dialog.setArguments(args);
dialog.setTargetFragment(targetFragment, 0);
dialog.show(targetFragment.getFragmentManager(), "tag");
}
public AlertDialogFragment() {}
@NonNull
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState)
{
Bundle args = getArguments();
String title = args.getString(ARG_TITLE, "");
String message = args.getString(ARG_MESSAGE, "");
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
}
})
.create();
}
它消除了(类的)用户熟悉组件内部结构的需要,并使使用变得非常简单:
AlertDialogFragment.showAlert(title, message, this);
附注:在我的情况下,我需要一个简单的警告对话框,所以这就是我所创建的。您可以将该方法应用于Yes/No或您需要的任何其他类型。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件