布局文件xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DialogActivity" > <Button
android:id="@+id/plainDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通Dialog" /> <Button
android:id="@+id/plainDialogEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog按钮事件集中处理" /> <Button
android:id="@+id/inputDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入框" /> <Button
android:id="@+id/listDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" /> <Button
android:id="@+id/radioDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" /> <Button
android:id="@+id/checkboxDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" /> <Button
android:id="@+id/diyDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义布局对话框" /> </LinearLayout>

Activity文件:

普通的dialog:

 private void plainDialogDemo() {

         Button plainBtn = (Button) findViewById(R.id.plainDialog);
plainBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this)
.setTitle("删除")
.setMessage("确定删除指定数据")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"确定了", Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
}
}).setCancelable(false).show();
}
});
}

效果如下:

输入文本框的dialog:

 private void inputDialog() {
Button inputBtn = (Button) findViewById(R.id.inputDialog);
inputBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText et = new EditText(DialogActivity.this);
new AlertDialog.Builder(DialogActivity.this)
.setTitle("请输入数字")
.setView(et)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
et.getText(),
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null)
.setCancelable(false).show();
}
});
}

效果如下:

列表dialog:

private void listDialogDemo() {
Button listBtn = (Button) findViewById(R.id.listDialog);
listBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this).setTitle("列表对话框")
.setItems(names, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DialogActivity.this,
names[which], Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
}

效果如下:

单选dialog:

 private void radioDialogDemo() {
Button radioButton = (Button) findViewById(R.id.radioDialog);
radioButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this)
.setTitle("列表对话框")
.setSingleChoiceItems(names, 0,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) { selecteName = names[which];
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) { Toast.makeText(DialogActivity.this,
selecteName, Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
}

效果如下:

多选dialog:

 private void checkDialogDemo() {
Button checkBtn = (Button) findViewById(R.id.checkboxDialog);
checkBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
final boolean[] selected = new boolean[] { true, false, true };
new AlertDialog.Builder(DialogActivity.this)
.setMultiChoiceItems(
names,
selected,
new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) { }
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
StringBuilder sb = new StringBuilder(
"你选择了:");
for (int i = 0; i < names.length; i++) {
if (selected[i]) {
sb.append(names[i]);
}
}
Toast.makeText(DialogActivity.this,
sb.toString(), 1).show();
}
}).setNegativeButton("取消", null).show();
}
});
}

效果如下:

自定义dialog:

 private void customDialogDemo() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.diylayout);
ImageButton ok = (ImageButton) window.findViewById(R.id.btnok);
ok.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "关闭了",
Toast.LENGTH_SHORT).show();
dlg.dismiss();
}
});
}

自定义布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/dialogimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/dialog_bg" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:text="自定义的dialog" /> <ImageButton
android:id="@+id/btnok"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/close_dialog" /> </RelativeLayout>

效果如下:

Android中Dialog对话框的更多相关文章

  1. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  2. android从Dialog对话框中取得文本文字

    android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...

  3. Android中Dialog

    在Android中,Dialog是一个非常重要的UI, 它可以方便的给用户提示,用最简洁的方式向用户展示信息, 以下的图片是Dialog的一个整体架构,通过它,可以总体对Dialog有一个很清晰的认识 ...

  4. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

  5. Android 中Dialog的使用

    本文是参考ProAndroid的第10章Working with Dialogs的内容,在合适的地方添加了作者自己的一些见解最终成文. Android 中的对话框是一个展示在当前窗口上的小一号的窗口, ...

  6. [原]MFC中DIALOG(对话框)程序响应加速键(快捷键)

    [原]MFC中DIALOG(对话框)程序响应加速键(快捷键) 2014-8-6阅读266 评论0 新建一个对话框程序,项目名为Test,删除默认确定,取消和静态文本框控件.添加一个按钮,Caption ...

  7. android中常见对话框之一AlertDialog

    在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...

  8. Android中Dialog的使用

    上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用. 1.什么是Dialog? Dialog就是对话框的一种方式! 在Android开发中.我们常常会须要在Android ...

  9. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

随机推荐

  1. 《think in python》学习-9

    think in python think in python -9 案例分析:文字游戏 从文本文件中读取文字 作者提供了一个文本文件words.txt 地址 本章后面案例也会用带该文件中的词组 fi ...

  2. UVa 821 Page Hopping

    题意: 给出一个有向图,求所有路径(两点间的最短路径)的平均值. 分析: 用floyd求两点间的最短距离,然后求平均就好. 代码: #include <iostream>#include ...

  3. 操作Sql数据库帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. 标准C++的string类使用

    原文:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含#include & ...

  5. 表现层技术以及Freemaker使用教程

    表现出计数以及Freemaker详解 在java领域,表现层技术主要有三种:jsp.freemarker.velocity.jsp是大家最熟悉的技术优点: 1.功能强大,可以写java代码 2.支持j ...

  6. file_get_contents()的另一种使用方法

    今天在网上看到一篇挺不错的文章,拿过来保存学习一下.本文源地址为:http://www.kuitao8.com/20140727/2867.shtml $data = file_get_content ...

  7. mysql优化(3) 集群配置

    两台服务器 192.168.187.131 192.168.187.132 1.主从配置 131为主 132为从 在131下 vim /etc/my.cnf [mysqld] datadir=/var ...

  8. 自定义TabHost,TabWidget样式

    先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:

  9. 使用PDO执行SQL语句exec()、query()

    在PHP脚本中,通过PDO执行SQL查询与数据库进行交互,可以分为三种不同的策略,使用哪一种方法取决于你要做什么操作. 1.使用PDO::exec()方法 当执行INSERT.UPDATE和DELET ...

  10. python sys.exit()函数说明

    sys.exit()函数是通过抛出异常的方式来终止进程的,也就是说如果它抛出来的异常被捕捉到了的话程序就不会退出了. #!/usr/bin/python #!coding:utf-8 import s ...