告诉大家如何记忆用户单选和复选对话框的选项
package com.example.alertdialogdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { String[] single_list = {"男", "女", "女博士", "程序员"}; String[] hobby_list = {"音乐", "篮球", "足球", "吉他"}; boolean[] checkedList = {false, false, false, false}; private int checkedItem = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initEvent1(); initEvent2(); initEvent3(); } private void initEvent1() { findViewById(R.id.dialog_btn1).setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog1(); } }); } private void initEvent2() { findViewById(R.id.dialog_btn2).setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog2(); } }); } private void initEvent3() { findViewById(R.id.dialog_btn3).setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog3(); } }); } /** * 显示确认对话框 */ private void showDialog1() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("确认对话框"); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("这是确认的内容"); //设置内容 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确认内容", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "取消内容", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create(); //获取dialog dialog.show(); } private void showDialog2() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择性别"); builder.setIcon(R.drawable.ic_launcher); builder.setSingleChoiceItems(single_list, checkedItem, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String str = single_list[which]; checkedItem = which; //记忆单选框选项 Toast.makeText(MainActivity.this, "此人是" + str + "!", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } private void showDialog3() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("爱好"); builder.setIcon(R.drawable.ic_launcher); builder.setMultiChoiceItems(hobby_list, checkedList, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { String str = hobby_list[which]; checkedList[which] = isChecked; //记住复选框选项 if (isChecked) { Toast.makeText(MainActivity.this, "I like " + str + "!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "I dislike " + str + "!", Toast.LENGTH_SHORT).show(); } } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }); AlertDialog dialog = builder.create(); dialog.show(); } }
这是我的代码,大家可以参考下,记忆复选框,只需在开始定义一个布尔类型的数组,初始值都设置为false,然后在点击事件里将isChecked的值赋给定义的数组即可;单选只需定义一个int型的变量,方法和复选类似,还不懂的可以仔细看看代码。