对话框

创建Dialog的两种方式:

1. 使用Dialog类或其子类,包括DialogFragment

2. 在Activity中使用Dialog主题(theme)

下面是使用Dialog类的一个例子:

// Create the new Dialog.
Dialog dialog = new Dialog(MyActivity.this);
// Set the title.
dialog.setTitle(“Dialog Title”);
// Inflate the layout.
dialog.setContentView(R.layout.dialog_view);
// Update the Dialog’s contents.
TextView text = (TextView)dialog.findViewById(R.id.dialog_text_view);
text.setText(“This is the text in my dialog”);
// Display the Dialog.
dialog.show();

也可以使用Dialog的子类,例如常用的AlertDialog:

Context context = MyActivity.this;
String title = “It is Pitch Black”;
String message = “You are likely to be eaten by a Grue.”;
String button1String = “Go Back”;
String button2String = “Move Forward”;
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle(title);ad.setMessage(message);
ad.setPositiveButton(button1String, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int arg1) {
    eatenByGrue();
  }
});
ad.setNegativeButton(button2String, new DialogInterface.OnClickListener(){
  public void onClick(DialogInterface dialog, int arg1) {
    //do nothing
  }
});

还可以设置是否显示Cancel按钮:

ad.setCancelable(true);
ad.setOnCancelListener(new DialogInterface.OnCancelListener() {
  public void onCancel(DialogInterface dialog) {
    eatenByGrue();
  }
});

使用内置的Dialog

Android中提供了这些现成的Dialog供我们使用:

1. CharacterPickerDialog

2. DatePickerDialog

3. TimePickerDialog

4. ProgressDialog

使用Fragment

Android中几乎所有的可见部分都可以放到Fragment中,我认为Fragment相对于View的一大优点是可以控制自己的生命周期。

public class MyDialogFragment extends DialogFragment {
  private static String CURRENT_TIME = “CURRENT_TIME”;
  public static MyDialogFragment newInstance(String currentTime) {
    // Create a new Fragment instance with the specified parameters.
    MyDialogFragment fragment = new MyDialogFragment();
    Bundle args = new Bundle();
    args.putString(CURRENT_TIME, currentTime);
    fragment.setArguments(args);
    return fragment;
  }
}

Android 4 学习(21):对话框的更多相关文章

  1. Android开发学习之对话框浅析

    对话框式程序运行中弹出的窗口.Android系统中有四种默认的对话框:警告对话框AlertDialog.进度对话框ProgressDialog.日期选择对话框DatePickerDialog以及时间选 ...

  2. Android:日常学习笔记(6)——探究活动(3)

    Android:日常学习笔记(6)——探究活动(3) 活动的生命周期 返回栈 Android中的活动是可以叠加的,我们每启动一个新活动,就会覆盖在原来的活动上,点击Back以后销毁最上面的活动,下面的 ...

  3. Android开发学习总结(一)——搭建最新版本的Android开发环境

    Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...

  4. Android Animation学习(二) ApiDemos解析:基本Animatiors使用

    Animator类提供了创建动画的基本结构,但是一般使用的是它的子类: ValueAnimator.ObjectAnimator.AnimatorSet ApiDemos中Animation部分是单独 ...

  5. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  6. 我的Android 4 学习系列之文件、保存状态和首选项

    目录 使用Shared Preference 保留简单的应用程序数据 保存回话间的Activity实例数据 管理应用程序首选项和创建Preference Screen 保存并加载文件以及管理本地文件系 ...

  7. Android开发学习之路--网络编程之xml、json

    一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...

  8. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  9. Android开发学习总结——搭建最新版本的Android开发环境

    原文出自:https://www.cnblogs.com/xdp-gacl/p/4322165.html#undefined 最近由于工作中要负责开发一款Android的App,之前都是做JavaWe ...

  10. Android布局学习——android:gravity和android:layout_gravity的区别

    [Android布局学习系列]   1.Android 布局学习之——Layout(布局)详解一   2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)   3.And ...

随机推荐

  1. Ubuntu连接手机步骤

    第一次使用adb之前,需要在home/.android里新建adb_usb.ini文件:0x1782. 注:adb已在安装系统后装好,手机要处于开机状态 查看设备命令: $ adb devices 正 ...

  2. Buildroot 外部编译器配置

    /****************************************************************************** * Buildroot 外部编译器配置 ...

  3. swift 函数参数——实参与形参

    实参 在传入函数之前已有明确定义. 具体定义为 func funcName( actualParameter: Type ) {} 形参 参数只在此函数内有效并可使用,函数外不需要有明确定义. 具体定 ...

  4. Mac终端建立替身 并置于桌面或Finder中

    前情 Xcode存放log的文件夹路径忒长了,且需要用终端才能查看.所以就想制作个替身,放在Finder中便于查看. going on command+space打开terminal 一直cd...进 ...

  5. 因为yii2中jquery位置默认在最下方,可将自定义js位置放在下方

    因为yii2中jquery位置默认在最下方,可将自定义js位置放在下方,这样就可以执行当页面加载完触发动作.记录下方式,查找方便 <?php $this->beginBlock('test ...

  6. js缓动函数

    tween: { easeInQuad: function(pos){ return Math.pow(pos, 2); }, easeOutQuad: function(pos){ return - ...

  7. CentOS 7.4中firewall防火墙详解和配置以及切换为iptables防火墙

    转载:https://blog.csdn.net/xlgen157387/article/details/52672988 一.firewall介绍 CentOS 7中防火墙是一个非常的强大的功能,在 ...

  8. ubuntu16安装最新版docker

    ubuntu16.04安装最新版docker.docker-compose.docker-machine https://www.cnblogs.com/tianhei/p/7802064.html ...

  9. ft,dtft,dft的关系(转载)

    很多同学学习了数字信号处理之后,被里面的几个名词搞的晕头转向,比如DFT,DTFT,DFS,FFT,FT,FS等,FT和FS属于信号与系统课程的内容,是对连续时间信号的处理,这里就不过多讨论,只解释一 ...

  10. FastAdmin 中使用 Oder by if 强行将某一类放到前面

    FastAdmin 中使用 Oder by if 强行将某一类放到前面 问题来源社区问题 1,查了一些资料2,做了测试. 如下表,我想把 snake 单独放到开始. 可以使用以下查询语句(默认为 AS ...