Dialog分三个区域
Title
Content area:不能同时放message和list
Action buttons:最多三个
创建AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog.Builder里面的方法
setTitle()
setMessage()
setPositiveButton()
setNegativeButton()
setNeutralButton()
create()
添加Button
setPositiveButton()
setNegativeButton()
setNeutralButton()
*
A traditional single-choice list 单选
setItems()
setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
setAdapter(),可能还需要Loader异步加载数据
A persistent single-choice list (radio buttons) 单选
setSingleChoiceItems()
A persistent multiple-choice list (checkboxes) 多选
setMultiChoiceItems()
这里需要创建一个属性,存储点击的项目
private ArrayList mSelectedItems;
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
创建自定义布局
方法一:
创建xml布局文件
使用LayoutInflater创建布局文件的View
setView(…)
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
方法二:将activity作为dialog
创建一个activity
设置activity的xml属性