Android开发之基于DialogFragment创建对话框的方法示例
作者:迟做总比不做强 发布时间:2023-10-03 21:17:12
标签:Android,DialogFragment,对话框
本文实例讲述了Android基于DialogFragment创建对话框的方法。分享给大家供大家参考,具体如下:
/**
* 使用DialogFragment创建对话框
* @description:
* @author ldm
* @date 2016-5-12 下午2:00:01
*/
public class FragmentAlertDialog extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
// 初始化Button及设置监听
button = (Button) findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 弹出对话框
showDialog();
}
});
}
void showDialog() {
// DialogFragment 创建对话框
DialogFragment newFragment = MyAlertDialogFragment
.newInstance(R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), "dialog");
}
public void doPositiveClick() {
Log.i("FragmentAlertDialog", "Positive click!");
}
public void doNegativeClick() {
Log.i("FragmentAlertDialog", "Negative click!");
}
/**
* 自定义弹出对话框DialogFragmet
*
* @description:
* @author ldm
* @date 2016-5-12 下午1:54:31
*/
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;
}
/**
* DialogFragment需要实现onCreateView或者onCreateDIalog方法。
* onCreateView():使用定义的xml布局文件展示Dialog。
* onCreateDialog():利用AlertDialog或者Dialog创建出Dialog。
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())//创建一个Dialog
.setIcon(R.drawable.alert_dialog_icon)//设置图标
.setTitle(title)//设置标题
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {//确认(OK)按钮
public void onClick(DialogInterface dialog,
int whichButton) {
((FragmentAlertDialog) getActivity())
.doPositiveClick();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,//取消(Cancel)按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((FragmentAlertDialog) getActivity())
.doNegativeClick();
}
}).create();
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="top|center_horizontal"
android:text="Example of displaying an alert dialog with a DialogFragment"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/show" >
</Button>
</LinearLayout>
开源代码:https://github.com/ldm520/ANDROID_API_DEMOS
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/true100/article/details/51383639


猜你喜欢
- 入住博客园4年多了,一直都是看别人的博客,学习别人的知识,为各个默默无私贡献自己技术总结的朋友们顶一个;这几天突然觉得是时候加入该队列中,贡
- 今天写一个小程序有一个给图片加上阴影的需求,记得WPF的Effect中就有阴影特效,就打算用它了。代码如下:using (var image
- 近来总是接触到 IoC(Inversion of Control,控制反转)、DI(Dependency Injection,依赖注入)等编
- 前言在引入 fl_chart 绘制图表的时候,看到插件有下面这样的动效,随机散乱的圆点最后组合成了 Flutter 的 Logo,挺酷炫的。
- Dotnet中嵌入资源(位图、图标或光标等)有两种方式,一是直接把资源文件加入到项目,作为嵌入资源,在代码中通过Assembly的GetMa
- 老大让我check out 一个分支,可我在idea 右下角找了半天也没找到最后才发现:因为是刚创建的分支,我得先更新一下项目,连这个都不懂
- 概述递归:指在当前方法内调用自己的这种现象。递归的分类:递归分为两种,直接递归和间接递归。直接递归称为方法自身调用自己。间接递归可以A方法调
- 环境介绍 IDEA我用的是2020.2Gradle 安装参考 Gradle安装配置我这安装的是6.6.1C:\Users\herion>
- 本文实例讲述了java执行Linux命令的方法。分享给大家供大家参考。具体实现方法如下:public class StreamGobbler
- 想要实现无限轮播,一直向左滑动,当到最后一个view时,会滑动到第一个,无限…可以自己写ViewPager然后加handler先实现自动滚动
- Visual Studio 2019 Vue项目 创建成功后可看到如下结构 Visual Studio 2019配置vue项目具体文件结构如
- 目录1、简介2、访问修饰符3、原则总结1、简介访问修饰符是Java语法中很基础的一部分,但是能正确的使用Java访问修饰符的程序员只在少数。
- 上一小节我们学习了FastThreadLocal的创建和get方法的实现逻辑, 这一小节学习FastThreadLocal的set方法的实现
- 前提:你的电脑是AMD处理器,想使用Android studio,自己的电脑系统是win10家庭版,在百度找到勾选hyper-v就能用,然后
- ??Chip监听选中状态的监听:setOnCheckedChangeListener,该监听只有设置了checkable 属性为true或者
- 前言:SpringBoot版本 : 2.2.6mybatis-generator-maven-plugin版本: 1.4.0plugin 使
- 本文实例讲述了C#实现IP摄像头的方法。分享给大家供大家参考。具体实现方法如下:#region IP摄像头代码/// <summary
- 前言Apache POI [1] 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Mi
- 1. 前言随着数据量和调用量的增长,用户对应用的性能要求越来越高。另外,在实际的服务中,还存在着这样的场景:系统在组装数据的时候,对于数据的
- Android studio 出现 Unsupported major.minor version 52.0解决办法 最近更新了