Android之AlertDialog应用
Android 中的对话框 dialog
对话框对于应用是必不可少的一个组件,在Android中也不例外,用于提示重要信息...
Android提供了丰富的对话框支持,它提供了4种常用的对话框形式:
- AlertDialog:警告对话框,使用最广泛功能最丰富的一个对话框
- ProgressDialog:进度条对话框,只是对进度条进行了简单的封装
- DatePickerDialog:日期对话框
- TimePickerDialog:时间对话框
这里简单介绍 AlertDialog 的使用方法:
activity_main 布局文件
<?xml version="1.0" encoding="utf-8"?>
<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="com.dragon.android.alertdialog.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="massage"
android:id="@+id/bt_msg"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sure"
android:id="@+id/bt_sure"
android:layout_below="@+id/bt_msg"
android:layout_alignRight="@+id/bt_msg"
android:layout_alignEnd="@+id/bt_msg" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText"
android:id="@+id/bt_editText"
android:layout_below="@+id/bt_sure"
android:layout_alignLeft="@+id/bt_sure"
android:layout_alignStart="@+id/bt_sure" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="singlechoice"
android:id="@+id/bt_singleChoice"
android:layout_below="@+id/bt_editText"
android:layout_centerHorizontal="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mutichoice"
android:id="@+id/bt_multiChoice"
android:layout_below="@+id/bt_singleChoice"
android:layout_centerHorizontal="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="imageview"
android:id="@+id/bt_imageView"
android:layout_below="@+id/bt_multiChoice"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_main
MainActivity
package com.dragon.android.alertdialog; import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
findViewById(R.id.bt_msg).setOnClickListener(this);
findViewById(R.id.bt_sure).setOnClickListener(this);
findViewById(R.id.bt_editText).setOnClickListener(this);
findViewById(R.id.bt_singleChoice).setOnClickListener(this);
findViewById(R.id.bt_multiChoice).setOnClickListener(this);
findViewById(R.id.bt_imageView).setOnClickListener(this);
} @Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (v.getId()) {
case R.id.bt_msg:
builder
.setTitle("Title") // 设置标题
.setMessage("this is a massage") // 设置提示信息
.setPositiveButton("sure", null) // 参数1:按钮名;参数2:监听器
.show();
break;
case R.id.bt_sure:
builder
.setTitle("Sure")
.setMessage("Sure?")
.setPositiveButton("sure", null)
.setNegativeButton("cancel", null)
.show();
break;
case R.id.bt_editText:
builder
.setTitle("Please input")
.setView(new EditText(this)) // 此方法显示一个View
.setPositiveButton("sure", null)
.setNegativeButton("cancel", null)
.show();
break;
case R.id.bt_singleChoice:
builder
.setTitle("Please choice")
.setSingleChoiceItems(new String[]{"A", "B", "C"}, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 单选框,点击一个则自动退出
dialog.dismiss();
}
}) // 参数1:选择框的选项数;参数2:选择的选项ID;参数3:监听器
.setNegativeButton("cancel", null)
.show();
break;
case R.id.bt_multiChoice:
builder
.setTitle("Please choice")
.setMultiChoiceItems(new String[]{"A", "B", "C"}, null, null) // 类同单选
.setPositiveButton("sure", null)
.setNegativeButton("cancel", null)
.show();
break;
case R.id.bt_imageView:
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher); // 设置图片资源
builder
.setTitle("Image")
.setView(imageView) // 此方法显示一个View
.setPositiveButton("sure", null)
.show();
break;
}
}
}
Android之AlertDialog应用的更多相关文章
- Android之AlertDialog.Builder详解
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; ...
- 【Android】Android在AlertDialog使用大全
package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...
- [转]好文章:Android的AlertDialog详解
refer:http://www.2cto.com/kf/201205/131876.html AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDial ...
- 【Android】Android中AlertDialog对话框的使用实例
package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...
- Android 自定义AlertDialog退出对话框
Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...
- 家庭记账本app进度之android中AlertDialog的相关应用以及对日期时间的相关操作(应用alertdialog使用的谈话框)
对于AlertDialog的相关知识: 1.创建构造器AlertDialog.Builder的对象: 2.通过构造器对象调用setTitle.setMessage.setIcon等方法构造对话框 ...
- (原)android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因
摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我 ...
- Android设置AlertDialog点击按钮对话框不关闭(转)
(转自:http://blog.csdn.net/winson_jason/article/details/8485524) 当我们在用到Android alertDialog创建对话框 的时候,我们 ...
- Android:AlertDialog对话框
1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...
随机推荐
- share登录Samba可读可写(适合虚拟机下学习使用)
直接配置 smb.conf ( path = /etc/samba/smb.conf ). 首先,进入到 samba 文件夹: cd /etc/samba/ 备份 smb.conf: mv smb.c ...
- 如何让WEBAPI 能够进行跨越访问
WebApi域名 http://localhost:11565 当部署好WebApi时,直接通过浏览器进行本地访问 这里是GET请求,此时访问成功 在部署一个Web,域名:http://localho ...
- 个人纪录(初)----配置文件.properties的简单读取
本文为个人文本纪录. demo:::: 1.创建普通的java项目:这实例项目名字叫properties. 2.创建.properties文件:src目录下创建XX.properties文件,识别&q ...
- js中,全局变量与直接添加在window属性的区别
在js中定义的全局变量是挂在window下的,而window的属性也一样,那么这两者有什么区别呢? 其实这两者还是有小小的区别的,全局变量是不能通过delete操作符删除的,而直接定义在window上 ...
- git 小结
git cherry-pick de0ec64 可将另一个分支中的提交 cherry-pick到当前分支来
- JS中的属性和变量的区别
在很多文章中都说变量其实就是属性,但是它们之间有一定的区别,例如: 在全局作用域下, var a = "hello"; b = "hello"; 从字面上看,它 ...
- 输出MYSQL所有SQL语句
在my.cnf中的mysqld段增加如下参数,然后重启MYSQL: log-output = FILE general_log = 1 general_log_file = "D:/Visu ...
- net之工作流工程展示及代码分享(记录)
http://www.cnblogs.com/thanks/p/4183235.html
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- php 序列化、json
序列化 和 反序列化1. serialize和unserialize函数 2. json_encode 和 json_decode 使用JSON格式序列化和反序列化3. var_export 和 ev ...





