我想制作一个对话片段
,询问你确定吗?"回复是/否".
I'd like to make a dialog fragment
that asks "Are you sure?" with a "yes/no" reply.
我查看了文档,它真的很冗长,到处都是这个地方,解释了如何制作高级对话框,但没有完整的代码来制作一个简单的hello world"类型的对话框.大多数教程使用已弃用的对话框系统.官方博客似乎过于复杂且难以理解.
I've looked at the documentation and it's really verbose, going all over the place, explaining how to make advanced dialog boxes, but no intact code on making a simple 'hello world' kind of dialog box. Most tutorials utilize the deprecated dialog box system. The official blog seems to be unnecessarily complicated and difficult to understand.
那么,创建和显示一个真正基本的警报对话框的最简单方法是什么?使用支持库的奖励积分.
So, what's the simplest way to create and display a really basic Alert Dialog? Bonus points if it's using the support library.
DialogFragment 其实就是一个包装了对话框的片段.您可以通过在 DialogFragment 的 onCreateDialog() 方法中创建并返回对话框来将任何类型的对话框放入其中.
A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.
这是一个示例 DialogFragment:
Heres an example DialogFragment:
class MyDialogFragment extends DialogFragment{
Context mContext;
public MyDialogFragment() {
mContext = getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Really?");
alertDialogBuilder.setMessage("Are you sure?");
//null should be your on click listener
alertDialogBuilder.setPositiveButton("OK", null);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
}
创建对话调用:
new MyDialogFragment().show(getFragmentManager(), "MyDialog");
并从某处关闭对话框:
((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();
只需更改导入以使用支持库类,所有这些代码都将与支持库完美配合.
All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.
这篇关于最简单的是/否对话框片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!