在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框;

实例如下:

1.效果图:

2.XML代码:

 <RelativeLayout 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: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=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="信息提示框" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="弹出单选按钮框" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="弹出多选按钮框" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="弹出一组列表框" /> <Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:text="弹出进度条框" /> <Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button5"
android:text="自定义布局框" /> </RelativeLayout>

3.java代码:

 package com.example.alertdilog;

 import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; import android.os.Bundle; public class MainActivity extends Activity implements OnClickListener { private Button Buttton1;
private Button Buttton2;
private Button Buttton3;
private Button Buttton4;
private Button Buttton5;
private Button Buttton6; private EditText mUserNamEditText;
private EditText mPwdEditText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView(); } private void initView() {
Buttton1 = (Button) this.findViewById(R.id.button1);
Buttton2 = (Button) this.findViewById(R.id.button2);
Buttton3 = (Button) this.findViewById(R.id.button3);
Buttton4 = (Button) this.findViewById(R.id.button4);
Buttton5 = (Button) this.findViewById(R.id.button5);
Buttton6 = (Button) this.findViewById(R.id.button6); Buttton1.setOnClickListener(this);
Buttton2.setOnClickListener(this);
Buttton3.setOnClickListener(this);
Buttton4.setOnClickListener(this);
Buttton5.setOnClickListener(this);
Buttton6.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1: // 信息提示框
AlertDialog();
break;
case R.id.button2:
AlertRadioDialog(); // 弹出单选按钮
break;
case R.id.button3:
AlertCheckBoxDialog();
break;
case R.id.button4:
AlertListDialog();
break;
case R.id.button5:
AlertProgressDialog();
break;
case R.id.button6:
AlertziDialog();
break; default:
break;
}
} private void AlertziDialog() {
// 动态加载布局生成View对象
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View view = layoutInflater.inflate(R.layout.login_alert, null);
mUserNamEditText = (EditText) view.findViewById(R.id.edit_username);
mPwdEditText = (EditText) view.findViewById(R.id.edit_password); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("登陆");
builder.setIcon(R.drawable.ic_launcher).create();
builder.setView(view);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.show();
} private void AlertListDialog() {
// 指定下拉列表的显示数据
final String[] cities = { "广州", "上海", "北京", "香港", "澳门", "深圳", "天津",
"武汉", "兰州", "长沙", "重庆", "西安", "成都", "安徽" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择一个城市");
// 设置一个下拉的列表选择项
builder.setItems(cities, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择的城市为:" + cities[which],
Toast.LENGTH_SHORT).show();
}
});
builder.show();
} private void AlertRadioDialog() {
final String[] sex = { "男", "女", "保密" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择性别");
builder.setIcon(android.R.drawable.ic_dialog_info).create();
builder.setSingleChoiceItems(sex, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择的是:" + sex[which],
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", null);
builder.show();
} private void AlertCheckBoxDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择爱好");
builder.setIcon(R.drawable.ic_launcher).create();
builder.setMultiChoiceItems(new String[] { "打球", "谈朋友", "看小说", "写代码" },
null, null);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.show();
} private void AlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("信息提示");
builder.setMessage("删除后不能回复,确认要删除吗?");
builder.setIcon(R.drawable.ic_launcher).create();
builder.setPositiveButton("确认", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.finish();
}
});
builder.setNegativeButton("取消", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
} private void AlertProgressDialog() {
ProgressDialog mypDialog = new ProgressDialog(MainActivity.this);
// 设置ProgressDialog 标题
mypDialog.setTitle("信息提示框");
// 设置ProgressDialog 提示信息
mypDialog.setMessage("Loading..........");
// 设置ProgressDialog 标题图标
mypDialog.setIcon(R.drawable.ic_launcher);
// 设置ProgressDialog 进度条进度
mypDialog.setProgress(59);
// 设置ProgressDialog 的进度条是否不明确
mypDialog.setIndeterminate(true);
// 设置ProgressDialog 是否可以按退回按键取消
mypDialog.setCancelable(true);
// 显示出来来
mypDialog.show();
} }

Android学习之Dialog的更多相关文章

  1. Android学习自定义Dialog

    Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...

  2. Android学习笔记-Dialog详解

    1.对话框的使用 1.1AlertDialog的显示 简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词): PositiveButton(确认按钮);NeutralButton(忽略按钮) ...

  3. 【转】Pro Android学习笔记(四六):Dialog(3):对话框弹对话框

    目录(?)[-] 帮助提示框的实现 实现再弹框 再谈fragment管理器 提示框的按钮Help,将触发弹出新的帮助提示框. 帮助提示框的实现 帮助提示框的实现很简单,利用重写onCreateView ...

  4. 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考

    目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...

  5. 【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog

    目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和 ...

  6. 【转】 Pro Android学习笔记(四五):Dialog(2):DialogFragment

    [-] 重写onCreateView 通过onCreateView设置UI和按键反馈 信息保存 重写onCreateDialog DialogFragment的实例newInstance()已经在上一 ...

  7. android学习日记03--常用控件Dialog

    常用控件 9.Dialog 我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框 对话框,要创建对话框之前首先要创建Bui ...

  8. Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法

    新建了一个Android Studio工程,在MainActivity的主界面中添加了两个按钮,点击其中一个按钮用来启动 NormalActivity,点击另一按钮用来启动DialogActivity ...

  9. Android学习路线总结,绝对干货

    title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...

随机推荐

  1. 【转载】Exchange 2010配置与安装实用手册

    Exchange 2010配置与安装实用手册 在Exchange 2010配置的时候主要分三大部分,这分别是网络配置.准备存储以及相关的安装策略和过程.同时还需要注意和其他的Windows软件相协调. ...

  2. JUnit4时间(超时)测试实例

    “时间测试”是指,一个单元测试运行时间是否超过指定的毫秒数,测试将终止并标记为失败. import org.junit.*; /** * JUnit TimeOut Test * @author yi ...

  3. ZooKeeper在分布式应用中的作用

    作者:陈叶皓(携程邮轮研发部软件架构师) 是不是要在标题的“作用”之前加上“重要”两个字,我犹豫了一下,zookeeper提供的功能是如此的重要,以至于如果你在应用中不使用它,早晚也会在你的应用中去实 ...

  4. Python——dummy_thread( _dummy_thread in Python 3.+)

    dummy_thread 模块在Python 3中改称为 _dummy_thread 模块,Python 的 2to3 工具能够帮你自动的更改旧代码中的模块名称.不过更推荐使用高层次的 dummy_t ...

  5. php后台管理员权限相关表结构

    admin管理员表 id int(11) 用户id username varchar(128) 用户名 password varchar(128) 管理员密码 name varchar(50) 管理员 ...

  6. Winform控件学习笔记【第三天】——ListBox

    1. 属性事件列表: SelectionMode    组件中条目的选择类型,即多选(Multiple).单选(Single) Rows             列表框中显示总共多少行 Selecte ...

  7. python程序的输入输出(acm的几个小程序)

    1,  A+B Problem : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1000 #! ...

  8. @Mock与@InjectMocks的区别

    @Mock: 创建一个Mock. @InjectMocks: 创建一个实例,简单的说是这个Mock可以调用真实代码的方法,其余用@Mock(或@Spy)注解创建的mock将被注入到用该实例中. 注意: ...

  9. Linux-centos6.8下关闭防火墙

    一.临时关闭防火墙 1. 查看防火墙的状态 [root@vpnSS ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ...

  10. ColorSense颜色检测器

    下载地址:https://github.com/omz/ColorSense-for-Xcode 修改OMColorSense.xcodeproj工程里的OMColorHelper.m文件的内容,实现 ...