AlertDialog的几种用法
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的几种用法的更多相关文章
- using 的三种用法
using 有哪三种用法? 1)引入命名空间. 2)给命名空间或者类型起别名. 3)划定作用域.自动释放资源,使用该方法的类型必须实现了 System.IDisposable接口,当对象脱离作用域之后 ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- Wix 安装部署教程(十五) --CustomAction的七种用法
在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
- Js闭包常见三种用法
Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- Service的两种用法及其生命周期
先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...
- C#中this的 四种 用法
C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// < ...
- js正则表达式中的问号几种用法小结
这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...
随机推荐
- POJ3104 Drying —— 二分
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- sql server中like无法匹配下划线问题解决方案
在sql server的like中下划线类似于通配符%,所以无法使用like '%_%'来匹配下划线,可以通过以下两种办法实现匹配下划线 1.使用转义字符escape like '%\_%' esca ...
- 深入理解dispatch_sync
关于GCD的基础知识,之前写过一篇博客,详见GCD基础知识.虽然之前已经梳理过了,但对很多知识点的理解仍然不够透彻…写这篇博客的原因是在阅读AFNetworking代码时遇到一些奇怪的代码. 如下: ...
- bzoj1025 [SCOI2009]游戏——因数DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1025 这篇博客写得真好呢:https://www.cnblogs.com/phile/p/4 ...
- python-day-9- 进程-异步IO\
本节内容 进程 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 多进程multiprocessing multiprocessing is a package that sup ...
- 【旧文章搬运】炉子给的SYSTEM_HANDLE_TYPE有点错误
原文发表于百度空间,2008-12-03========================================================================== 今天写程序 ...
- 安装并配置JAVA环境
详见百度经验 http://jingyan.baidu.com/article/0202781175839b1bcc9ce529.html
- [技术分享]利用MSBuild制作msf免杀的后门
文章github上有公开现成的shellcode,这就是shellcode 我这次选择了32位的那个版本来进行演示 需要改写的是shellcode那部分: 选择CobaltStrike:payload ...
- IT兄弟连 JavaWeb教程 URI、URL
URI介绍 URI(Uniform Resource Identifier),是统一资源标识符的缩写,是一个用于标识某一个Web资源名称的字符串,该标识允许用户对任何资源通过特定的协议进行交互.Web ...
- 第十三篇 .NET高级技术之事件
案例:定一个Person类,定一个监听年龄变化的事件,当本命年的时候祝贺一下.触发事件的地方要判断一下是不是事件null 事件语法:event Mydelegate mdl; 加了event关键字实现 ...