xml代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单的dialog"
android:onClick="dialog_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表的dialog"
android:onClick="dialog_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选的dialog"
android:onClick="dialog_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选的dialog"
android:onClick="dialog_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义View的dialog"
android:onClick="dialog_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用adapter的dialog"
android:onClick="dialog_6"/>
</LinearLayout>

java代码:

 package com.example.lesson7_3_id19_alertdialog;

 import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dialog_1(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round); builder.setTitle("标题栏");
builder.setMessage("正文部分,简单的文本");
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.setNeutralButton("中立",null);
AlertDialog alertDialog = builder.create();
alertDialog.show(); }
private String [] item = {"游戏","运动","电影","旅游","看书"};
public void dialog_2(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setItems(item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
}
});
// 取消可以不添加
//builder.setNegativeButton("取消",null);
AlertDialog alertDialog = builder.create();
alertDialog.show(); }
int index;
public void dialog_3(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
index = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
// 设置boolean数组所有的选项设置默认没选
boolean[] bools = {false,false,false,false,false};
public void dialog_4(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
bools[which] = isChecked;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < item.length; i++) {
if (bools[i]) {
sb.append(item[i] + " ");
}
}
Toast.makeText(MainActivity.this, "选择了" + sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_5(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("连接wifi");
final EditText et = new EditText(this);
et.setHint("请输入密码");
et.setSingleLine(true);
builder.setView(et);
builder.setNegativeButton("取消",null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String password = et.getText().toString();
if (password.equals("123456")) {
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show(); }
public void dialog_6(View v){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("使用适配器");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}

AlertDialog的几种用法的更多相关文章

  1. using 的三种用法

    using 有哪三种用法? 1)引入命名空间. 2)给命名空间或者类型起别名. 3)划定作用域.自动释放资源,使用该方法的类型必须实现了 System.IDisposable接口,当对象脱离作用域之后 ...

  2. c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast

    C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...

  3. Wix 安装部署教程(十五) --CustomAction的七种用法

    在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...

  4. Android Intent的几种用法全面总结

    Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...

  5. Js闭包常见三种用法

        Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...

  6. operator 的两种用法

    C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...

  7. Service的两种用法及其生命周期

    先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...

  8. C#中this的 四种 用法

    C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// < ...

  9. js正则表达式中的问号几种用法小结

    这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...

随机推荐

  1. JS判断按时间跳转到相应的页面

    <!--时间段跳转js--><script language="javaScript" type="text/javascript"> ...

  2. position属性中的绝对定位和相对定位

    absolute(绝对定位):1.如果没有父级DIV,则会根据浏览器原始点去定位,而且跟他相邻的DIV会忽略它,定位后则可用TRBL(top,right,bottom,left)去布局.注意:TRBL ...

  3. Ubuntu上命令行下卸载软件

    sudo apt-get --purge remove 软件名 (加了--purge表示会删除配置) sudo apt-get autoremove (这个命令后面文章有解释) dpkg -l (查看 ...

  4. 【CQOI2009】中位数图

    [题目链接] 点击打开链接 [算法] 将小于m的数看作-1,大于m的看作1 然后求前缀和,如果区间[l,r]的中位数是m,显然有 : sum(r) - sum(l-1) = 0 因此,只需m的位置之前 ...

  5. JQ的双向数据绑定

    把渲染页面封装在function里面 在修改后直接调用,但是如果有alert();一定要注意function的摆放位置,正常应该是在alert前面,应为如果在后面会影响重新渲染的效果

  6. Laravel 在 Nginx 中的参考配置两份

    此份参考自网络: server { listen 80; server_name laravel.app; root /项目目录/public; index index.php index.html ...

  7. Code:NFine框架

    ylbtech-Code:NFine框架 1.返回顶部 1.         2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出 ...

  8. 【推荐】 体验SubSonic

    SubSonic简介 SubSonic配置 利用sonic.exe来生成代码 通过Substage来生成代码 简单操作示例 1.SubSonic简介 一句讲完就是:SubSonic就是一个ORM开源框 ...

  9. python整数与IP地址转换 [转]

    我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import socket >>> ...

  10. jqGrid 编辑完数据后能返回到当前位置的方法

    jqGrid 是一个js的jquery组件,虽然不轻便,但功能还是蛮强大的,也比较方便使用.在数据加载后,经常需要对其中的记录进行编辑,修改完后再返回时需要看到修改后的数据,一般采取重新加载的方法re ...