对话框

创建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. linux 文件上传&软件安装(rpm)

    文件的上传与下载(linux -linux ) 实例1:从远处复制文件到本地目录命令:scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /o ...

  2. 软件包 com.baidu.location

    http://developer.baidu.com/map/loc_refer/index.html?com/baidu/location/package-summary.html

  3. ios 延迟调用 && UIImageView && UILabel && UISegmentedControl && UISwitch && UISlider

    // //  ViewController.m //  UI_Lesson3 // //  Created by archerzz on 15/8/13. //  Copyright (c) 2015 ...

  4. matlab图像处理注意溢出!先要im2double!

    imagedata_comb=imagedata_ebic*addnumber_ebic+imagedata_sem*addnumber_sem; %注意溢出啊!!!uint8最大值是255,也就是说 ...

  5. 将 WPF、UWP 以及其他各种类型的旧样式的 csproj 文件迁移成新样式的 csproj 文件

    写过 .NET Standard 类库或者 .NET Core 程序的你一定非常喜欢微软为他们新开发的项目文件(对于 C#,则是 csproj 文件).这种文件非常简洁,组织一个庞大的项目也只需要聊聊 ...

  6. 使 WPF 支持触摸板的横向滚动

    微软终于开始学苹果一样好好做触摸板了(就是键盘空格键下面那一大块).然而鉴于以前没有好好做,以至于 WPF 程序甚至都没有对触摸板的横向滚动提供支持(竖向滚动是直接使用了 MouseWheel,汗-- ...

  7. 用eclipse运行java程序显示找不到main class,网上的方法都试了,还是不行,有没有知道怎么解决的呀!

    编译器问题,jdk版本不对,Window --> Preferences -->Java --> compiler中的compiler compliance level,这里选你当前 ...

  8. Makefile常用知识点

    格式 目标:最终要去生成的文件, 定格写,后面是冒号(冒号后面是依赖) 依赖:用来生成目标的源材料 命令:加工的方法,命令前面一定是Tab, make的过程就是使用命令将依赖加工成目标的过程 工作原理 ...

  9. C# 线程间不能调用剪切板的问题

    最近做一个项目,需要用到线程,而且要用到剪切板,创建了一个子线程之后发现在子线程中剪切板上获取不到数据,当时特别纳闷,上网查资料,最后终于搞定,现将解决方法归纳如下: 第一步: public void ...

  10. spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

    Tomcat容器启动失败,找到 debug日志一看: Context initialization failed org.springframework. beans.factory.xml.XmlB ...