设置单选列表只需AlertDialog.Builder里面的setSingleChoiceItems 来设置即可

实现步骤如下:

第一步:用来显示列表内容的

res/values/array.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="hobby"> <item>篮球</item> <item>足球</item> <item>排球</item> </string-array> </resources>

第二步:还是定义一个输入框和一个按钮

res/layout/single_choice_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是一个单选列表对话框" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示单选列表对话框" /> </LinearLayout>

第三步:

产生一个单选列表对话框,首先需要new 一个AlertDialog.Builder作为对话框内容的载体,然后通过setSingleChoiceItems将

builder与array.xml中的数据关联,需要通过DialogInterface.OnClickListener对列表单选单击事件进行处理,

为了保存单选列表项中的选中数据,需要单独写一个类,且类中需要加一个选中了哪一个列表项的属性which,

代码如下

src/com/dialog/activity/SingChoiceDialogActivity.java

package com.dialog.activity; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SingChoiceDialogActivity extends Activity { private final int SING_CHOICE_DIALOG = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.single_choice_dialog_layout); Button button = (Button) findViewById(R.id.button); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { showDialog(SING_CHOICE_DIALOG); } }; button.setOnClickListener(listener); } @Override protected Dialog onCreateDialog(int id) { Dialog dialog = null; switch(id) { case SING_CHOICE_DIALOG: Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.basketball); builder.setTitle("体育爱好"); final ChoiceOnClickListener choiceListener = new ChoiceOnClickListener(); builder.setSingleChoiceItems(R.array.hobby, 0, choiceListener); DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { EditText editText = (EditText) findViewById(R.id.editText); int choiceWhich = choiceListener.getWhich(); String hobbyStr = getResources().getStringArray(R.array.hobby)[choiceWhich]; editText.setText("你选择了 " + hobbyStr); } }; builder.setPositiveButton("确定", btnListener); dialog = builder.create(); break; } return dialog; } private class ChoiceOnClickListener implements DialogInterface.OnClickListener { private int which = 0; @Override public void onClick(DialogInterface dialogInterface, int which) { this.which = which; } public int getWhich() { return which; } } }

效果图:

源码下载地址:http://download.csdn.net/source/3221459

更多相关文章

  1. 自定义字体主题+自定义背景+设置radiobutton的字体的选中状态的
  2. ButterKnife的使用详解
  3. 更新ADT20后出现This template depends on Android(安卓)Support
  4. Android高仿IOS和QQ的弹出对话框
  5. android 仿IOS添加小组件的拖拽排序列表,使用SwipeRecyclerView
  6. Android实现iPhone晃动撤销输入功能 Android仿微信摇一摇功能
  7. Android中获取后台正在运行的应用列表(附源码)
  8. Android开发之如何使用通知提醒功能(Notifications)
  9. Android(安卓)打开系统文件管理器,并返回选中文件的路径

随机推荐

  1. Android中当前时间的动态显示
  2. Android的简单图形和view刷新
  3. android 创建 删除桌面快捷方式
  4. Android语音识别的具体实现实例
  5. Android Wi-Fi Disable Roaming in wpa_s
  6. NFC相关知识与材料(一)
  7. 【Android(安卓)开发教程】保存状态等信
  8. Android根据电量变化为不同图片的方法【
  9. android EditText 监听复制粘贴等操作
  10. Android屏幕休眠禁止或清除