Android之复选框对话框用法实例分析
作者:Ruthless 时间:2023-10-03 05:07:03
本文实例讲述了Android之复选框对话框用法。分享给大家供大家参考。具体如下:
main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:text=""
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false" />
<Button android:text="显示复选框对话框"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
array.xml数组
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="hobby">
<item>游泳</item>
<item>打篮球</item>
<item>登山</item>
</string-array>
</resources>
AlertActivity类
package com.ljq.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AlertDialog extends Activity {
private EditText editText;
private final static int DIALOG=1;
boolean[] flags=new boolean[]{false,false,false};//初始复选情况
String[] items=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items=getResources().getStringArray(R.array.hobby);
editText=(EditText)findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 显示对话框
showDialog(DIALOG);
}
});
}
/**
* 创建复选框对话框
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//设置对话框的图标
builder.setIcon(R.drawable.header);
//设置对话框的标题
builder.setTitle("复选框对话框");
builder.setMultiChoiceItems(R.array.hobby, flags, new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
String result = "您选择了:";
for (int i = 0; i < flags.length; i++) {
if(flags[i]){
result=result+items[i]+"、";
}
}
editText.setText(result.substring(0, result.length()-1));
}
});
//添加一个确定按钮
builder.setPositiveButton(" 确 定 ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
//创建一个复选框对话框
dialog=builder.create();
break;
}
return dialog;
}
}
运行结果:
希望本文所述对大家的Android程序设计有所帮助。
标签:Android,对话框
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring纯注解配置实现代码示例解析
2021-06-05 18:54:28
springboot反爬虫组件kk-anti-reptile的使用方法
2022-01-09 14:12:59
![](https://img.aspxhome.com/file/2023/6/85896_0s.png)
SpringBoot Admin 如何实现Actuator端点可视化监控
2022-12-09 01:40:51
![](https://img.aspxhome.com/file/2023/5/94225_0s.png)
Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
2023-03-17 11:09:11
![](https://img.aspxhome.com/file/2023/5/80855_0s.jpg)
详解Java的Hibernate框架中的Interceptor和Collection
2023-08-18 04:02:55
![](https://img.aspxhome.com/file/2023/2/72572_0s.png)
为什么wait和notify必须放在synchronized中使用
2022-07-20 07:32:39
![](https://img.aspxhome.com/file/2023/7/71187_0s.webp)
spring中bean的生命周期详解
2021-11-29 23:31:02
spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面(推荐
2023-07-28 17:39:16
Golang+Android基于HttpURLConnection实现的文件上传功能示例
2023-10-27 06:19:46
C# 计算DataTime的4种时间差的方法(相差天数、相差小时、相差分钟、相差秒)
2022-12-08 10:37:07
java selenium教程之selenium详细介绍
2023-07-30 13:53:52
java 命名空间 命名规则第1/2页
2021-06-06 14:20:19
java查询mongodb中的objectid示例
2022-03-06 08:59:21
Android仿微信朋友圈添加图片的实例代码
2021-06-26 13:19:05
![](https://img.aspxhome.com/file/2023/8/138018_0s.gif)
SpringBoot深入了解日志的使用
2023-01-06 15:40:34
![](https://img.aspxhome.com/file/2023/7/65177_0s.png)
Spring Security 中如何让上级拥有下级的所有权限(案例分析)
2022-01-28 16:55:26
![](https://img.aspxhome.com/file/2023/9/68019_0s.jpg)
Android 安全加密:非对称加密详解
2021-08-03 18:39:43
![](https://img.aspxhome.com/file/2023/9/138759_0s.png)
Android实现数字跳动效果的TextView方法示例
2023-05-24 16:54:29
![](https://img.aspxhome.com/file/2023/4/137784_0s.gif)
深入Android SQLite 事务处理详解
2023-07-17 08:32:17
SpringBoot整合腾讯云COS对象存储实现文件上传的示例代码
2021-06-01 03:25:14
![](https://img.aspxhome.com/file/2023/0/113520_0s.jpg)