栗子4
多选列表项对话框
只要调用AlertDialog.Builder的setMultiChoiceItems()方法即可创建一个多选列表项对话框。调用setMultiChoiceItems()方法时既可传入Cursor(相当于数据库查询结果集)作为参数。
下面的程序呈现调用了setMultiChoiceItems()方法来创建带多选列表项的对话框。
主要代码:
/*多选列表项对话框*/
public void multiChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//设置对话框标题
.setTitle("多选列表项对话框")
//设置图标
.setIcon(R.drawable.ic_launcher_background)
//设置单选列表项,默认选中第二项(索引为1)
.setMultiChoiceItems(items, new boolean[]{true,true},null);
//添加确定按钮
setPositiveButton(builder);
//添加取消按钮
setNegativeButton(builder).create().show();
}
调用AlertDialog.Builder的setMultiChoiceItems()方法添加多选列表项时,需要传入boolean[]参数,改参数有两个作用:
1.设置初始化时选中那些列表项;
2.该boolean[]类型的参数还可以用于动态地获取多选列表中列表项的选中状态。
栗子5
自定义View对话框
login.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="10pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="sss"
android:hint="请填写登录账号"
android:selectAllOnFocus="true"
android:inputType="number"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="10pt"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="www"
android:hint="请填写密码"
android:inputType="numberPassword"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码"
android:textSize="10pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的手机号码"
android:selectAllOnFocus="true"
android:inputType="phone"
/>
</TableRow>
</TableLayout>
主要代码:
public void customView(View source)
{
TableLayout LoginForm =(TableLayout)getLayoutInflater()
.inflate(R.layout.login,null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("自定义view对话框")
.setView(R.layout.login)
.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}