安卓 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 ...
随机推荐
- 【XJOI-NOIP16提高模拟训练9】题解。
http://www.hzxjhs.com:83/contest/55 说实话这次比赛真的很水..然而我只拿了140分,面壁反思. 第一题: 发现数位和sum最大就是9*18,k最大1000,那么su ...
- 解决xshell连接linux总是异常断开的问题?
找到etc下面的profile文件,可以看到有下面一行: TMOUT=;export TMOUT 注释掉即可解决. #TMOUT=;export TMOUT
- spring 注入失败
最近发现autowired注入总是失败. 总结下: 一个bean 要么都通过getter setter加上配置文件配置注入. <bean id="temResetService&quo ...
- Tomcat打印运行时日志(控制台),访问日志,启动日志
1.sh catlina.sh run以控制台形式输出 2.sever.xml.配置acesslog,设置访问日志输出 Tomcat的访问日志是靠org.apache.catalina.valves. ...
- MyEclipse快捷键记录
MyEclipse快捷键 ------------------------------------- MyEclipse 快捷键1(CTRL) ---------------------------- ...
- AcmeAir安装AI探针--SaaS版
一.安装SaaS版AI探针准备工作: 1.准备好可用的docker版AcmeAir应用 2.在SaaS官网tpm.oneapm.com上已注册可用的账号 3.登录tpm.oneapm.com,点击选择 ...
- JLink and JTAG等接口简介
1.JTAG JTAG用的计算机的并口,JTAG也是一种国际标准测试协议(IEEE 1149.1兼容),主要用于芯片内部测试.现在多数的高级器件都支持JTAG协议,如DSP.FPGA器件等. 标准的J ...
- VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)-软件开发-鸡啄米 http://www.jizhuomi.com/software/143.html 鸡啄米在上一讲中 ...
- JVM垃圾回收机制总结(2) :基本算法概述
1.引用计数收集器 (Reference Counting) 引用计数是垃圾收集的早期策略.在这种方法中,堆中每一个对象都有一个引用计数.一个对象被创建了,并且指向该对象的引用被分配给一个变量,这个对 ...
- Oracle ->> 查看分区表的每个分区的数据行分布情况
ora_hash函数用来返回分区号,而dbms_rowid.rowid_object()函数用来返回object_id , ) part_id ,count(*) from sales_fact_pa ...