Android安卓多选对话框奔溃问题解决!!!

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33615172/article/details/77744305


解决反复点击奔溃问题关键代码:


完整代码:


布局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>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 初始化控件

		bt = (Button) findViewById(R.id.bt);

		// 为按钮设置点击事件

		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog();// 弹出对话框
			}
		});

	}

	// 创建多选对话框

	public void dialog() {

		AlertDialog.Builder builder = new Builder(MainActivity.this);

		builder.setIcon(R.drawable.ic_launcher);

		builder.setTitle("多选对话框");

		builder.setMultiChoiceItems(strs, flags,
				new OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						// TODO Auto-generated method stub

						if (isChecked) {

							set.add(which);

						} else {

							set.remove(which);

						}

					}
				});

		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public 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() {

			@Override
			public 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;

	}

}




效果:




没有更多推荐了,返回首页