1.单选对话框
在这之前要定义:
String habit[] = { “读书”, “打球”, “听歌” };
boolean defaultState[] = { true, false, false };
List list = new ArrayList();
public void singleChooseDlg() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("单选对话框");
// final int which1=-1;
// 关于这块为啥使用数组类型的变量,详见:
// http://blog.csdn.net/lovewaterman/article/details/40342669
final int[] temp=new int[1];
builder.setSingleChoiceItems(habit,0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(MainActivity.this,habit[which], 0).show();
temp[0] = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,habit[temp[0]], 0).show();
}
});
builder.show();
}
2.多选对话框
public void multipleChooseDlg() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("多选对话框");
// builder.setSingleChoiceItems(items, checkedItem, listener)
builder.setMultiChoiceItems(habit, defaultState,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// 反复点的时候使用一个数组记录状态,之后再进行遍历即可
defaultState[which] = isChecked;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String string = "";
for (int i = 0; i < habit.length; i++) {
if (defaultState[i]) {
string += habit[i];
}
}
Toast.makeText(MainActivity.this, string, 0).show();
}
});
builder.show();
}
3.自定义对话框
1.在Layout文件夹下新建一个dialog.xml自定义对话框的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否自动跳转下一题"
android:textSize="30sp" >
</TextView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否开启解释"
android:textSize="30sp" >
</TextView>
<RadioGroup
android:id="@+id/radio_group"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="30sp"
android:textColor="#ffffff"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="女"
android:textColor="#ffffff"/>
</RadioGroup>
</LinearLayout>
代码编写如下:
TextView tView=null;
public void customDlg(){
View titleView=(View)LayoutInflater.from(MainActivity.this).inflate(R.layout.title, null);
final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);
tView=(TextView) view.findViewById(R.id.textView);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setTitle("自定义对话框")
.setCustomTitle(titleView);
builder.setView(view);
final RadioGroup radioGroup =(RadioGroup) view.findViewById(R.id.radio_group);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 有RadioGroup获取选中的RadioButton的对象。由对象获取对应的文本
RadioButton radioButton =(RadioButton)view.findViewById(radioGroup.getCheckedRadioButtonId());
// Toast.makeText(MainActivity.this,radioButton.getText(), 0).show();
}
});
builder.show();
}