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 ...
随机推荐
- μC/OS-Ⅲ中的临界段代码
临界段代码(critical sections),也叫临界区(critical region),是指那些必须完整连续运行,不可被打断的代码段.μC/OS-Ⅲ系统中存在大量临界段代码.采用两种方式对临界 ...
- iOS程序启动过程
First, the function creates the main application object (step 3 in the flowchart). If you specify ni ...
- 清除Linux OS 缓存
1.查看内存使用情况 [root@ip---- tpch_2_17_0]# free -m total used free shared buffers cached Mem: -/+ buffers ...
- 最近用到js筛选一个url的域名部分(草创)
var TLD = ['com','net','org','gov','edu','mil','biz','name','info','mobi','cn','hk']; var host = ''; ...
- 总结Themida / Winlicense加壳软件的脱壳方法
总结下Themida/ Winlicense (TM / WL) 的脱壳方法. 1, 查看壳版本,这个方法手动也可以,因为这个壳的版本号是写在程序里面的,在解压后下断点即可查看,这里有通用的脚本,我 ...
- 64位计算机安装setuptool
Python Version 2.7 required, which was not found in the registry 新建注册表信息:HKEY_LOCAL_MACHINE\SOFTWARE ...
- J2EE、J2SE、J2ME是什么意思?
本文介绍Java的三大块:J2EE.J2SE和J2ME.J2SE就是Java2的标准版,主要用于桌面应用软件的编程:J2ME主要应用于嵌入是系统开发,如手机和PDA的编程:J2EE是Java2的企业版 ...
- 我是一只IT小小鸟读后感
当老师推荐我读这本书的时候,并不想看,因为我不喜欢机械的东西,然而阅读几章后,对这本书有了其他看法.不知不觉竟把它看完!看完这本<我是一只IT小小鸟>,我感触很深. 在书中我明白了很多,大 ...
- web前端基础篇⑨
1.web端.app端 目前实现响应式布局,主要用以下两种方式.CSS原生代码响应式布局 @media screen.bootstrap移动设备优先.自带框架. 兼容性 用原生代码的话 根据不同的屏幕 ...
- git中.gitignore配置项不起作用-解决办法
在某个git项目中,.gitignore忽略了*.iml,但是git status命令依然列了出来,最后发现是由于git的缓存造成的. git rm -r --cached . git add . g ...





