谷歌建议我们使用DialogFragment而不是使用Fragments API创建一个简单的对话框,但是使用一个孤立的DialogFragment来创建一个简单的Yes-No确认消息框是荒谬的。在这种情况下,最佳实践是什么?
当前回答
你可以创建一般的DialogFragment子类,如YesNoDialog和OkDialog,并传入标题和消息,如果你在应用程序中使用对话框很多。
public class YesNoDialog extends DialogFragment
{
public static final String ARG_TITLE = "YesNoDialog.Title";
public static final String ARG_MESSAGE = "YesNoDialog.Message";
public YesNoDialog()
{
}
@Override
public Dialog 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.yes, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
}
})
.create();
}
}
然后使用以下命令调用它:
DialogFragment dialog = new YesNoDialog();
Bundle args = new Bundle();
args.putString(YesNoDialog.ARG_TITLE, title);
args.putString(YesNoDialog.ARG_MESSAGE, message);
dialog.setArguments(args);
dialog.setTargetFragment(this, YES_NO_CALL);
dialog.show(getFragmentManager(), "tag");
并在onActivityResult中处理结果。
其他回答
使用Dialog进行简单的是或否对话框。
当你需要更复杂的视图,其中你需要获得生命周期,如oncreate,请求权限,任何生命周期覆盖我将使用一个对话框片段。因此,您可以分离权限和对话框需要操作的任何其他代码,而不必与调用活动通信。
你可以创建一般的DialogFragment子类,如YesNoDialog和OkDialog,并传入标题和消息,如果你在应用程序中使用对话框很多。
public class YesNoDialog extends DialogFragment
{
public static final String ARG_TITLE = "YesNoDialog.Title";
public static final String ARG_MESSAGE = "YesNoDialog.Message";
public YesNoDialog()
{
}
@Override
public Dialog 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.yes, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
}
})
.create();
}
}
然后使用以下命令调用它:
DialogFragment dialog = new YesNoDialog();
Bundle args = new Bundle();
args.putString(YesNoDialog.ARG_TITLE, title);
args.putString(YesNoDialog.ARG_MESSAGE, message);
dialog.setArguments(args);
dialog.setTargetFragment(this, YES_NO_CALL);
dialog.show(getFragmentManager(), "tag");
并在onActivityResult中处理结果。
在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.
我可以建议简化一下@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或您需要的任何其他类型。
是的,使用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");
}
推荐文章
- 警告: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文件