?通过AlertDialog.Builder类的setSingleChoiceItems方法可以创建带单按钮的列表:方法如下:1、从资源文件中装载数据:public Builder setSingleChoiceItems(int itemsId, int checkedItem, final OnClickListener listener)2、从数据集中装载数据public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, final OnClickListener listener)3、从字符串数组中装载数据public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)4、从ListAdapter对象中装载数据public Builder setSingleChoiceItems(ListAdapter adpater, int checkedItem, final OnClickListener listener)??
?
?
?
package com.js.sinlgechoiceitemstest; import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast; public class SingleChoiceItemsTest extends Activity implements OnClickListener{ private String[] province = new String[] { "上海", "北京", "湖南", "湖北", "海南" }; private Button btnSingleChoiceList; // 单击事件对象的实例 private ButtonOnClick buttonOnClick = new ButtonOnClick(1); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSingleChoiceList = (Button) findViewById(R.id.btnSingleChoice); btnSingleChoiceList.setOnClickListener(this); } @Override public void onClick(View v) { showSingleChoiceButton(); } // 在单选选项中显示 确定和取消按钮 //buttonOnClickg变量的数据类型是ButtonOnClick,一个单击事件类 private void showSingleChoiceButton() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择省份"); builder.setSingleChoiceItems(province, 0, buttonOnClick); builder.setPositiveButton("确定", buttonOnClick); builder.setNegativeButton("取消", buttonOnClick); builder.show(); } private class ButtonOnClick implements DialogInterface.OnClickListener { private int index; // 表示选项的索引 public ButtonOnClick(int index) { this.index = index; } @Override public void onClick(DialogInterface dialog, int which) { // which表示单击的按钮索引,所有的选项索引都是大于0,按钮索引都是小于0的。 if (which >= 0) { //如果单击的是列表项,将当前列表项的索引保存在index中。 //如果想单击列表项后关闭对话框,可在此处调用dialog.cancel() //或是用dialog.dismiss()方法。 index = which; } else { //用户单击的是【确定】按钮 if (which == DialogInterface.BUTTON_POSITIVE) { //显示用户选择的是第几个列表项。 final AlertDialog ad = new AlertDialog.Builder( SingleChoiceItemsTest.this).setMessage( "你选择的地区是:" + index + ":" + province[index]).show(); //五秒钟后自动关闭。 Handler hander = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { ad.dismiss(); } }; hander.postDelayed(runnable, 5 * 1000); } //用户单击的是【取消】按钮 else if (which == DialogInterface.BUTTON_NEGATIVE) { Toast.makeText(SingleChoiceItemsTest.this, "你没有选择任何东西", Toast.LENGTH_LONG); } } } }}?