Simplest yes/no dialog fragment(最简单的是/否对话框片段)
问题描述
我想制作一个对话片段
,询问你确定吗?"回复是/否".
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.
这篇关于最简单的是/否对话框片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:最简单的是/否对话框片段


- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01