1 安卓弹出对话框是复选框的代码
- package com.example.b;
- import android.os.Bundle;
- import android.preference.MultiSelectListPreference;
- import android.app.Activity;
- import android.view.Menu;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity 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.activity_main);
- items=getResources().getStringArray(R.array.hobby);
- editText=(EditText)findViewById(R.id.msg);
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @SuppressWarnings("deprecation")
- public void onClick(View v) {
- // 显示对话框
- showDialog(1);
- }
- });
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- Dialog dialog = null;
- switch (id) {
- case 1:
- Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("title");
- builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- flags[which]=isChecked;
- String results = "";
- for (int i = 0; i < flags.length; i++) {
- if(flags[i])
- {
- results=results+items[i];
- }
- }
- editText.setText(results);
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- dialog = builder.create();
- break;
- default:
- break;
- }
- return dialog;
- }
- }
2 layout
- <LinearLayout 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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/msg2"
- android:text="@string/hello_world" />
- <EditText android:text=""
- android:id="@+id/msg"
- 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>
3 加入 多选框的选项
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="hobby">
- <item>游泳</item>
- <item>打篮球</item>
- <item>登山</item>
- </string-array>
- </resources>
4 效果图