安卓 Dialogs(对话框)
转载自:http://www.apkbus.com/home.php?mod=space&uid=679028&do=blog&id=61197
对话框是一个小的窗口用以提示用户做出决定或输入额外的信息。对话框不会填满屏幕并且通常被用作模态事件,要求用户做出行动才能继续下去。
AlertDialog一个可以展示 标题,三个按钮,可选择项的列表或自定义布局 的对话框。DatePickerDialog or TimePickerDialog一个使用预先定义好的UI,允许用户选择一个日期或时间 的对话框android.app.DialogFragment.。创建对话框碎片(Dialog Fragment)
DialogFragment:管理的:public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); }}
创建警告对话框(Alert Dialog)

AlertDialog::// 1. 通过它的构造器实例化一个 AlertDialog.BuilderAlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // 2.将设置方法都集合在一起来设置对话框的属性builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title); // 3. 从create()中获得AlertDialogAlertDialog dialog = builder.create();添加按钮
setPositiveButton() 和setNegativeButton()方法AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());// Add the buttonsbuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User clicked OK button } });builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } });// Set other dialog properties... // Create the AlertDialogAlertDialog dialog = builder.create();DialogInterface.OnClickListener ,该监听器定义了当用户点击该按钮时的操作。AlertDialog.中。也就是说,不能有多个“positive”按钮。添加列表

- 一个传统的单选列表
- 一个持续的单选列表(单选按钮)
- 一个持续的多选列表(复选框)
- //持续的说明可以保存选项
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_color) .setItems(R.array.colors_array, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // which 变量包含了被选中项的索引值 } }); return builder.create();}ListAdapter,返回你的列表,经常使用一个Loader来异步加载内容。这些在Building Layouts with an Adapter 和 Loaders开发向导中详细的讲解了。添加一个持久的复选/单选列表

setMultiChoiceItems() 或setSingleChoiceItems() 方法。ArrayList:中:@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { mSelectedItems = new ArrayList(); //跟踪被选项的地方 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // 设置对话框标题 builder.setTitle(R.string.pick_toppings) // 指定列表数组, 默认被选中项t (null for none),和选择项目时的监听器 .setMultiChoiceItems(R.array.toppings, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { // 如果用户选中了项目,将它加入被选择项 mSelectedItems.add(which); } else if (mSelectedItems.contains(which)) { // 否则,如果项目已经在数组中,移除它 mSelectedItems.remove(Integer.valueOf(which)); } } }) // Set the action buttons .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // 用户点击OK,因此建议mSelectedItems 结果存储在某处,或者从打开的对话框中返回它们。 // or return them to the component that opened the dialog ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { ... } }); return builder.create();}创建自定义布局(Custom Layout)

setView() 来添加进去。<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/header_logo" android:layout_width="match_parent" android:layout_height="64dp" android:scaleType="center" android:background="#FFFFBB33" android:contentDescription="@string/app_name" /> <EditText android:id="@+id/username" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="4dp" android:hint="@string/username" /> <EditText android:id="@+id/password" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="16dp" android:fontFamily="sans-serif" android:hint="@string/password"/></LinearLayout>LayoutInflater 并且调用inflate(),它的第一个参数是布局资源ID,第二个参数是布局的父view。之后你可以调用setView()来在该对话框中放置该布局。@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getActivity().getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.dialog_signin, null)) // Add action buttons .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // sign in the user ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { LoginDialogFragment.this.getDialog().cancel(); } }); return builder.create();}<activity> 配置元素中设置它的主题为Theme.Holo.Dialog :<activity android:theme="@android:style/Theme.Holo.Dialog" >传递事件出去 给对话框持有者
DialogFragment ,定义了一个接口,通过它传递事件给持有者的activity:public class NoticeDialogFragmentextends DialogFragment { /* 创建该对话框碎片引用的activity必须实现该接口 ,为了收到事件回调 public interface NoticeDialogListener { public void onDialogPositiveClick(DialogFragment dialog); public void onDialogNegativeClick(DialogFragment dialog); } // 使用该接口的实例来传递操作事件 NoticeDialogListener mListener; // 重写Fragment.onAttach()方法来实例化 NoticeDialogListener @Override public void onAttach(Activity activity) { super.onAttach(activity); // 验证持有者的activity 实现了回调接口 try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (NoticeDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } ...}public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{ ... public void showNoticeDialog() { // 创建一个 dialog fragment 引用并且展示它 DialogFragment dialog = new NoticeDialogFragment(); dialog.show(getSupportFragmentManager(), "NoticeDialogFragment"); } // dialog fragment 通过Fragment.onAttach()回调返回一个参数给改activity ,它被用来调用如下方法,这些方法定义在 NoticeDialogFragment.NoticeDialogListener接口 @Override public void onDialogPositiveClick(DialogFragment dialog) { // User touched the dialog's positive button ... } @Override public void onDialogNegativeClick(DialogFragment dialog) { // User touched the dialog's negative button ... }}public class NoticeDialogFragment extends DialogFragment { ... @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Build the dialog and set up the button click handlers AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Send the positive button event back to the host activity mListener.onDialogPositiveClick(NoticeDialogFragment.this); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Send the negative button event back to the host activity mListener.onDialogNegativeClick(NoticeDialogFragment.this); } }); return builder.create(); }}显示对话框
getFragmentManager() 从Fragment 中获得FragmentManager。例如:public void confirmFireMissiles() { DialogFragment newFragment = new FireMissilesDialogFragment(); newFragment.show(getSupportFragmentManager(), "missiles");}findFragmentByTag().来获得。显示一个全屏的对话框或者嵌入的碎片
DialogFragment 类提供你这些易用性,因为它仍可以像一个可嵌入的片段一样。onCreateView() 中加载该布局。public class CustomDialogFragment extends DialogFragment { /** 系统调用该方法来获得 DialogFragment's 布局, 不关心是否是作为一个对话框显示还是可嵌入的碎片 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout to use as dialog or embedded fragment return inflater.inflate(R.layout.purchase_items, container, false); } /** 系统调用该方法,仅当创建对话框布局时 */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 你必须覆盖该方法的唯一原因就是当使用 onCreateView() 来更改任何对话框属性时。例如,对话框默认包含一个标题,但是你的自定义布局或许不需要它。因此你可以移除对话框标题,但是你必须调用父类来获得该对话框。 Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); return dialog; }}public void showDialog() { FragmentManager fragmentManager = getSupportFragmentManager(); CustomDialogFragment newFragment = new CustomDialogFragment(); if (mIsLargeLayout) { // The device is using a large layout, so show the fragment as a dialog newFragment.show(fragmentManager, "dialog"); } else { // The device is smaller, so show the fragment fullscreen FragmentTransaction transaction = fragmentManager.beginTransaction(); // For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // To make it fullscreen, use the 'content' root view as the container // for the fragment, which is always the root view for the activity transaction.add(android.R.id.content, newFragment) .addToBackStack(null).commit(); }}<!-- Default boolean values --><resources> <bool name="large_layout">false</bool></resources><!-- Large screen boolean values --><resources> <bool name="large_layout">true</bool></resources>boolean mIsLargeLayout; @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);}一个activity作为对话框显示在大屏幕上
<activity>配置元素中应用Theme.Holo.DialogWhenLarge主题:<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >消散(dismissing)你的对话框
DialogFragment.中调用dismiss() 来手工的消散你的对话框。DialogFragment.中实现onDismiss()方法。onDismiss() 。然而,如果你调用Dialog.dismiss() 或DialogFragment.dismiss(),system 系统调用onDismiss()但不会调用onCancel()。因此当用户点击了你对话框上的posivity按钮来从view上移除该对话框时,通常调用dismiss() 。安卓 Dialogs(对话框)的更多相关文章
- Delphi XE5 Android Dialogs 对话框(模拟做了一套)
最近要在Android中使用对话框, 但发现无现成的, TOpenDialog等已经不支持移动设备,还好系统提供了一些文件目录函数可用,于是简单的模拟了一个,支持OpenDialog ,SaveDia ...
- 安卓Dialog对话框多次显示而闪退的解决办法
事情是这样子的,我在一个活动中自定义了一个AlertDialog,通过一个按钮点击即可弹出,而后来出现的情况是,第一次点击就没问题, 正常跳出,而第二次就直接程序闪退,然后报The specified ...
- Cordova指令
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld cordov ...
- Cordova搭建环境与问题小结
1.Cordova介绍: Apache Cordova是一套设备API,允许移动应用的开发者使用JavaScript来访问本地设备的功能,比如摄像头.加速计.它可以与UI框架(如jQuery Mobi ...
- WIN7下搭建CORDOVA环境
Cordova 环境搭建 1安装JDK 工具文件夹中:jdk目录 1)下载地址:http://www.oracle.com/technetwork/java/javase/downloads/inde ...
- QML官方系列教程——QML Applications
附网址:http://qt-project.org/doc/qt-5/qmlapplications.html 假设你对Qt的官方demo感兴趣,能够參考本博客的另一个系列Qt5官方demo解析集 每 ...
- Android 和 JS交互方法初探
起初有个需求,就是需要监听网页的图片点击,然后图片单独跳转到另一个页面单独显示 这里就需要用JS和Android Native方法之间的通信 先说上面的解决办法之前先引出两个Android的方法 1: ...
- Robot Framework(五)使用测试库
使用测试库 测试库包含那些最低级别的关键字,通常称为 库关键字,实际上与被测系统交互.所有测试用例总是使用某些库中的关键字,通常是通过更高级别的用户关键字.本节介绍如何使用测试库以及如何使用它们提供的 ...
- Cordova 常用命令及插件
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld 添加平台 co ...
随机推荐
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
比如JsonResult中返回return Json(models);的models结构如下: models返回含有四个集合的序列,每个集合的序列中又包含一个子集合序列“Child”. 问题是如果我们 ...
- PowerMockito(PowerMock用法)
网络上大部分是powermock 的用法, PowerMock有两个重要的注解: –@RunWith(PowerMockRunner.class) –@PrepareForTest( { YourCl ...
- NIM博弈的必胜取法
#include<stdio.h> ; int a[Max]; int main() { int i,n,ans; int tmp; while(scanf("%d", ...
- Hadoop基础教程之HelloWord
上一章中,我们把hadoop下载.安装.运行起来,最后还执行了一个Hello world程序,看到了结果.现在我们就来解读一下这个Hello Word. OK,我们先来看一下当时在命令行里输入的内容: ...
- ios开发--第三方整理
一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...
- php 写入数据到MySQL以及从MySQL获取数据,页面出现乱码的解决方法
现象如标题. 解决思路: 1确定数据库charset是否是utf-8 a. charset不是utf-8 1, 更改数据库charset为utf-8 ALTER DATABASE db_name DE ...
- 配置Linux系统网卡连接网络
本实验需要两台虚拟机来完成,环境如下: 主机名称 操作系统 ip地址 本地主机 RHEL7_64 192.168.157.134 -> 192.168.129.127 远程主机 CentOS 6 ...
- 京东商城发现了一枚Bug
我在京东上买了几本书,发现了一个BUG.. 买书的时候,我选了京东自营的书和京东其他店的书,合在一起购买,填写了开具发票. 然后,京东处理流程是,将上面一笔订单拆分成两笔,然后发票信息没有转到其他店那 ...
- MongoDB操作(.net)
一.查询 var doc = new Document(); 1.模糊查询var reg = new MongoRegex(".*" + qry.名称 + ".*&quo ...
- Hibernate 异常 —— No CurrentSessionContext configured
在使用 SessionFactory 的 getCurrentSession 方法时遇到如下异常 “No CurrentSessionContext configured ” 原因是: 在hibern ...