Android学习之Dialog
在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的更多相关文章
- Android学习自定义Dialog
Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...
- Android学习笔记-Dialog详解
1.对话框的使用 1.1AlertDialog的显示 简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词): PositiveButton(确认按钮);NeutralButton(忽略按钮) ...
- 【转】Pro Android学习笔记(四六):Dialog(3):对话框弹对话框
目录(?)[-] 帮助提示框的实现 实现再弹框 再谈fragment管理器 提示框的按钮Help,将触发弹出新的帮助提示框. 帮助提示框的实现 帮助提示框的实现很简单,利用重写onCreateView ...
- 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考
目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...
- 【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog
目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和 ...
- 【转】 Pro Android学习笔记(四五):Dialog(2):DialogFragment
[-] 重写onCreateView 通过onCreateView设置UI和按键反馈 信息保存 重写onCreateDialog DialogFragment的实例newInstance()已经在上一 ...
- android学习日记03--常用控件Dialog
常用控件 9.Dialog 我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框 对话框,要创建对话框之前首先要创建Bui ...
- Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法
新建了一个Android Studio工程,在MainActivity的主界面中添加了两个按钮,点击其中一个按钮用来启动 NormalActivity,点击另一按钮用来启动DialogActivity ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
随机推荐
- 用OpenGL实现粒子的随机运动
一.目的: 掌握OpenGL中粒子的绘制.随机数的使用 二.代码: Github地址 #include "stdafx.h" #include <GL/glut.h> ...
- Java数组搜索和比较
在Java中,如何搜索和比较数组? 示例 以下示例显示如何使用sort()和binarySearch()方法来完成任务.用户定义的方法printArray()用于显示输出 - package com. ...
- e742. 加入标签的可拖动能力
This example demonstrates how to modify a label component so that its text can be dragged and droppe ...
- e616. Determining If a Focus Lost Is Temporary or Permanent
A temporary focus-lost event occurs if the focus moves to another window. It's temporary because the ...
- (转)libhybris及EGL Platform-在Glibc生态中重用Android的驱动
原文地址:http://blog.csdn.net/jinzhuojun/article/details/41412587 libhybris主要作用是为了解决libc库的兼容问题,目的是为了在基于G ...
- Java调试那点事[转]
转自云栖社区:https://yq.aliyun.com/articles/56?spm=5176.100239.blogcont59193.11.jOh3ZG# 摘要: 该文章来自于阿里巴巴技术协会 ...
- Solaris10 修改hostname
修改/etc/nodename 及 /etc/hosts & /etc/hostname.<NIC Name> 这三个文件就可以了 重启
- [mysql]ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
From: http://m.blog.csdn.net/blog/langkeziju/13511411 我的MySQL版本为5.6.14版本,是二进制包安装的(虽然是二进制包安装的,但是以下问题同 ...
- unity3d 使用GL 方式画线
这个是画线部分 private Vector3[] linePoints; public int m_LineCount; public int m_PointUsed; public void Re ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...