Android Dialog AlertDialog

1、普通的对话框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
Dialog_progress.xml
public class MyDialog extends Dialog{
//必须要给构造方法
public MyDialog(Context context) {
//也可以在构建Dialog对象的时候就给指定Dialog样式
//使用主题来修改Dialog样式,在res/values/styles.xml中添加自定义主题
super(context,R.style.DialogTheme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这个可以不要标题。通过getWindow().requestFeature(featureId)方法
//getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_progress);
}
/**
* 一个Activity或者一个Dialog刚刚出现在用户面前的时候,焦点改变调用onWindowFocusChanged
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
}
}
MyDialog
// 普通对话框
public void dialog1(View v) {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("这是进度Dialog");
// 显示对话框
dialog.show(); // 关闭对话框用
// dialog.dismiss();
}
普通对话框
2、警告对话框AlertDialog setMessage

//AlertDialog setMessage
public void dialog2(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("警告!").setIcon(R.drawable.ic_launcher)
.setMessage("前方高能")
// 注意这个导的包是import android.content.DialogInterface.OnClickListener;
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了OK",
Toast.LENGTH_SHORT).show(); }
})
.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了Cancel",
Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Ignore", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了Ignore",
Toast.LENGTH_SHORT).show();
}
})
.create().show();
}
警告对话框,setMessage
3、菜单对话框AlertDialog setItem

//菜单选择, setItem 如果设置setMessage,那么只会显示Message
String[] setting = {"声音","存储","显示","应用","语言和输入法","流量使用情况","WLAN"};
public void dialog3(View v){
new AlertDialog.Builder(this)
.setTitle("设置")
.setIcon(R.drawable.setting)
//which代表第几项,item点击后自动关闭,不需要Button
.setItems(setting, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, setting[which], Toast.LENGTH_SHORT).show();
}
})
.create().show();
}
菜单对话框 setItem
4、单选对话框AlertDialog setSingleChoiceItems

//单选对话框,setItem
String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
int choice = 0;
public void dialog4(View v){
new AlertDialog.Builder(this)
.setTitle("爱好单选")
.setIcon(R.drawable.hobby)
//0代表默认选中第一个,选中不会自动关闭
.setSingleChoiceItems(hobby, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice = which;
}
})
//Button上的which永远为0,所以这里需要一个变量来保存选中的ItemID
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, hobby[choice], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel",null)
.create().show();
}
单选对话框 setSingleChoiceItems
5、多选对话框AlertDialog setMultiChoiceItem

String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
boolean[] bool = {false,false,false,false,false};
List<String> list = new ArrayList<String>();
public void dialog5(View v){
new AlertDialog.Builder(this)
.setTitle("爱好可多选")
.setIcon(R.drawable.hobby)
//默认选中了哪些,点击也不会自动关闭
.setMultiChoiceItems(hobby, bool, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
list.add(hobby[which]);
}else{
list.remove(hobby[which]);
}
}
})
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
}
}).create().show();
}
多选对话框AlertDialog setMultiChoiceItem
6、适配器对话框AlertDialog setAdapter

public void dialog6(View v){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hobby);
new AlertDialog.Builder(this)
.setTitle("适配器对话框")
//和setItem一样,选中之后对话框就自动消失,不需要Button
.setAdapter(adapter, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, hobby[which], Toast.LENGTH_SHORT).show();
}
}).create().show();
}
适配器对话框AlertDialog setAdapter
7、自定义对话框AlertDialog setView

8、关闭对话框AlertDialog dismisson

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="关闭对话框请点击关闭按钮"/> <TextView
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/> </LinearLayout>
Dialog_dismiss.xml
public void dialog8(View v){
View layout = getLayoutInflater().inflate(R.layout.dialog_dismiss, null);
TextView finish = (TextView) layout.findViewById(R.id.finish);
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("可关闭的对话框")
.setView(layout)
.create();
dialog.show();
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//有时候用户可能点到了外部,dialog就直接关闭了,而程序不知道,这时候就需要设置
dialog.setCancelable(false);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
}
});
}
关闭对话框 AlertDialog setView
Android Dialog AlertDialog的更多相关文章
- android Dialog&AlertDialog
Dialog dialog = new Dialog(context,R.style.AppBaseTheme); wifiView = AppData.inflater.inflate(R.layo ...
- Android DevArt2:Android 5.0下 Dialog&AlertDialog 并不会影响Activity的生命周期
先给出结论:Dialog和AlertDialog并不会影响到Activity的生命周期,但会影响到Activity的优先级. 核心代码: onCreated中: Resources resources ...
- android dialog
/** * @Title MenuTest.java * @package com.example.standardview * @since * @version 1.0.0 * @author V ...
- Android:AlertDialog对话框
1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...
- Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- 自定义android Dialog
1.自定义Dialog: import android.app.AlertDialog; import android.app.Dialog; import android.content.Conte ...
- 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 Dialog对话框
Dialog的基本方法 //创建Dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设 ...
随机推荐
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
http://blog.csdn.net/sun_zhicheng/article/details/24232129
- 30.SSH配置文件模板.md
[toc] 1.struts2 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...
- java测试1
发大水 package com.java1234.activiti.variable; import java.util.Date; import java.util.HashMap; import ...
- HTML5手机开发——滚动和惯性缓动
1. 滚动 以下是三种实现方式: 1) 利用原生的css属性 overflow: scroll div id= parent style = overflow:scroll; divid='conte ...
- 关于Python文档读取UTF-8编码文件问题
近来接到一个小项目,读取目标文件中每一行url,并逐个请求url,拿到想要的数据. #-*- coding:utf-8 -*- class IpUrlManager(object): def __in ...
- CSS实现table td中文字的省略与显示
所谓省略就是把多余的字以“...”显示出来,而显示则是当鼠标移动到td上时,把省略的字重新显示出来.对于一个table,兼容IE与FF.Chrome的省略方式CSS写法: table{ width:2 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- UML问题
1.在创建协作图时需要先确定参与者,而协作图的工具栏里是没有Actor的,这是需要先new Actor,然后把其拖动到工作区:实验过程中发现必须创建在用例视图下,若是创建在逻辑试图下那么根本无法继续操 ...
- SCALA表达示简例
作了解,感觉比一般的差距有点大哟. package com.hengheng.scala class HelloScala { } object HelloScala { def main(args: ...
- BAT之间的区别(学点网页编程,然后开始研究)
A: 阿里不就是靠网页起家的吗? T: 腾讯靠客户端. B: 百度是靠网页背后的算法技术支持- 最近网页技术又发布了很多新功能,而现在网页功能也已经很强大了. 不知道自己是不是老了,总觉得不喜欢网页( ...