安卓 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 ...
随机推荐
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
- poj 3114(强连通缩点+SPFA)
题目链接:http://poj.org/problem?id=3114 思路:题目要求很简单,就是求两点之间的花费的最短时间,不过有一个要求:如果这两个city属于同一个国家,则花费时间为0.如何判断 ...
- 使用secureCRT远程Linux,出现远程主机拒绝连接
1.查看是否开启远程连接, 控制面板->程序和功能->打开或关闭windows功能->勾选telnet服务器和telnet客户端2.cmd命令行输入telnet ip地址 端口号(比 ...
- asp.net跳转页面的三种方法比较
目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...
- 创建DB2数据库时报错--SQL1052N 数据库路径不存在(Windows)(转载)
用DB2 v9.7新建数据库的时候,默认路径为:D:\ 把缺省路径“写的是D:\XXX(此目录存在),新建时提示如下:SQL1052N 数据库路径 "D:\XXX" 不存在.如下: ...
- 一行代码设置TLabel.Caption的前世今生
第零步,测试代码: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'Hello World'; end ...
- Python3缩进对逻辑的影响
前话 我很佩服设计Python这门语言的人,因为这门语言的规则让我不得不写出美观的代码. Python的缩进要求是强制的,因为缩进不对直接影响了代码逻辑. 因为python写法相对其他编程语言简化许多 ...
- Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 一直只知道有除n*n的算法之外的求LIS,但是没学过,也没见过,今天终于学了一下,dp[i]表 ...
- Spring AOP术语
1.AOP术语 1)连接点(Joinpoint) 程序执行的某个特定位置:如类开始初始化前.类初始化后.类某个方法调用前.调用后.方法抛出异常后.一个类或一段程序代码拥有一些具有边界性 ...
- Data Base Oracle下载及安装
Oracle 下载及安装 一.官方下地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/in ...