Android安卓多选对话框奔溃问题解决!!!
来源:互联网 发布:阿里云邮箱域名解析 编辑:程序博客网 时间:2024/06/03 19:37
解决反复点击奔溃问题关键代码:
完整代码:
布局layout:
<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" tools:context=".MainActivity" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="107dp" android:text="点击弹出多选对话框" /></RelativeLayout>
Activity代码:
package com.fs.more_choose;import java.util.HashSet;import java.util.Set;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.DialogInterface.OnMultiChoiceClickListener;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button bt;private String[] strs = { "苹果", "香蕉", "水蜜桃", "西瓜" };private boolean[] flags = { false, false, false, false };// 用Set集合将选中的数据的下标保存(用List集合可能导致崩溃)private Set<Integer> set = new HashSet<Integer>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化控件bt = (Button) findViewById(R.id.bt);// 为按钮设置点击事件bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubdialog();// 弹出对话框}});}// 创建多选对话框public void dialog() {AlertDialog.Builder builder = new Builder(MainActivity.this);builder.setIcon(R.drawable.ic_launcher);builder.setTitle("多选对话框");builder.setMultiChoiceItems(strs, flags,new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {set.add(which);} else {set.remove(which);}}});builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub// 打印提示信息Toast.makeText(MainActivity.this, getChoose(),Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});builder.create();builder.show();}// 获取选择的数据public String getChoose() {String str = "";for (Integer item : set) {str += strs[item];// 集合中保存的是被选择的数据在数组strs中的下标,根据这些下标,从集合中取出数据,拼接到字符串中}if ("".equals(str)) {str = "没有选择任何数据";}return str;}}
效果:
阅读全文
1 0
- Android安卓多选对话框奔溃问题解决!!!
- iOS拨打电话对话框问题解决
- android 实现全局对话框中context的问题 .WindowManager$BadTokenException问题解决
- 关于 Android 运行时权限申请对话框无法正常弹出问题解决
- Android对话框
- Android对话框
- android 对话框
- android 对话框
- Android对话框
- Android对话框
- Android对话框
- Android对话框
- android对话框
- Android对话框
- android对话框
- Android对话框
- Android对话框
- android对话框
- Elasticsearch之分析。
- CAN通信机制
- opencv检测直线——投影法
- 第五十四篇:JAVA简易WEB服务器(四)
- forfiles命令详解
- Android安卓多选对话框奔溃问题解决!!!
- js中的sort排序问题
- android 签名验证
- WebService基础入门(java)
- 【技术】NGUI官网示例3--Menu
- python hasattr,getattr,setattr
- volatile关键字解析(转)
- SSM(二)Lucene全文检索
- 获取到某个元素的某个style中的属性。