安卓 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.Builder
AlertDialog.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 buttons
builder.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 AlertDialog
AlertDialog dialog = builder.create();
DialogInterface.OnClickListener
,该监听器定义了当用户点击该按钮时的操作。AlertDialog
.中。也就是说,不能有多个“positive”按钮。添加列表

- 一个传统的单选列表
- 一个持续的单选列表(单选按钮)
- 一个持续的多选列表(复选框)
- //持续的说明可以保存选项
@Override
public 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
:中:@Override
public 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()来在该对话框中放置该布局。@Override
public 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;
@Override
public 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 ...
随机推荐
- tomcat简介及原理解说
Tomcat简介 作者:杨晓(http://blog.sina.com.cn/u/1237288325) 目录: ----Tomcat背景 ----Tomcat目录 ----Tomcat类加载 --- ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- Android中级之网络数据解析一之xml解析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! --Comic Sans MS Xml解析具有跨平台性,语言无关性,易操作性,因此广受开发者的欢迎. ...
- 一步步来配置安卓开发环境ADTBundle
前提 安装JDK,并且要正确配置环境变量. 特别要注意安装配置完后,CMD命令行窗口里输入 java 和 javac 查看是否有内容输出,若提示找不到命令就是你JDK配置错误了 至于如何配置JDK看这 ...
- CentOS编译安装Python3
前话 最近想学一下一门新的高级语言,无意中看到用python仿AIphaGo的github项目,就决定是他了. AIphaGo的Git传送门: https://github.com/Rochester ...
- MyBatis学习总结_15_定制Mybatis自动代码生成的maven插件
==================================================================================================== ...
- SSIS ->> Logging
SSIS提供了Event Handler之外的另一种方法捕捉Event和获取需要的信息,这种方法是Logging.SSIS的Logging针对不同的组件可以提供比Event Handler更多的Eve ...
- 计算机IT求职学习List
1. 算法相关 1.1 <编程珠玑> 1.2 <编程之美> 这两本是最经典的了,这里面注重的是解决问题的思路,看的时候里面的问题要认真思考再参考解答.下面两本是对具体的面试题做 ...
- Delphi文件夹的操作
/// <remarks> /// 重命名文件夹 引用ShellAPI /// </remarks> function ReNameDirectort(SourceDirect ...
- C# Server.MapPath()
./当前目录 /网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot 应用程序虚拟目录为E:\wwwroot\company 浏览的页面路径为E:\wwwroot\ ...