详细参考:http://www.oschina.net/question/54100_32486
android dialog —— 单选列表对话框
2011-04-28 09:30:04| 分类: Android APK | 标签: |字号大中小 订阅
效果图:
设置单选列表只需 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.youkuaiyun.com/source/3221459
设置单选列表只需 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="
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.youkuaiyun.com/source/3221459
本文介绍如何在Android应用中创建一个单选列表对话框,包括定义列表项、设置对话框样式及处理用户选择等内容。
1005

被折叠的 条评论
为什么被折叠?



