安卓开发【UI组件】-对话框2

栗子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();

    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值