Android:<9>对话框练习

学完对话框,让我们来巩固一下,之前讲到对话框,大家还记得如何创建一个对话框吗?

我们使用了按钮的监听函数,我们通过Alertdialog这个对象创建的,记得是Alertdialog.Builder()函数;然后调用它的方法设置标题、图标、内容、按钮的操作,因为对话框就分为三个部分,一个是标题、一个是内容、一个就是按钮;上次我们做一个单选对话框,就是在列表中选一个然后点击确定使用吐司将内容显示,其他的复选框,登录框也是如此;

这一期在让我们来做一个差不多的程序巩固一下:

 我们就制作一个这样的界面,我用的是线性布局,因为简单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    tools:context=".Mydialog2Activity">

    <Button
        android:layout_width="80dp"
        android:layout_height="500dp"
        android:layout_gravity="center"
        android:onClick="btn_single2"
        android:text="单选框"
        android:textSize="50sp"/>

    <Button
        android:layout_width="80dp"
        android:layout_height="500dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_gravity="center"
        android:onClick="btn_multipy2"
        android:text="复选框"
        android:textSize="50sp"/>

    <Button
        android:layout_width="80dp"
        android:layout_height="500dp"
        android:layout_gravity="center"
        android:onClick="btn_login2"
        android:text="登录框"
        android:textSize="50sp"/>

</LinearLayout>

 所以你们也一定可以看懂,这里要注意的只有如何居中的问题,我使用了两个gravity一个是按钮的一个是主布局的,主布局为内容水平居中,按钮就全部居中,保险一点;

1、单选框:

 

这个界面有一个图标,这是自己找的,你们也可以按照自己喜好去阿里巴巴矢量图标库里面找一个,这个界面也挺好做的:

//1.1 编写单选框
    public void btn_single2(View view) {
        //1.2 创建一个水果数组
        String[] fruits = new String[]{"苹果","香蕉","水蜜桃","粑粑柑"};

        Dialog dialog = new AlertDialog.Builder(this).
                setTitle("选择你的喜欢的水果").
                setIcon(R.drawable.icon2).
                setSingleChoiceItems(fruits, index, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //1.4 将选项存储
                        index = i;
                    }
                }).
                setCancelable(false).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Mydialog2Activity.this, "你喜欢:"+fruits[index], Toast.LENGTH_SHORT).show();
                    }
                }).
                setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();
        dialog.show();
    }

这里我用的是级联操作,也就是创建Dialog对象,然后一直点,反正设置属性一直点就可以,结束的时候加上create()就可以,然后在调用一下show()函数,和上一期的基本没有差别,不过要注意一点,上一期也讲过不知道大家记得吗?就是我们的初始值,我们需要设置为全局变量,因为这是内部类,需要提高级别;好了,实现输出内容的逻辑就是在我们的确定按钮的监听函数里面吐司下表为index的数值,这个index是会变化的,在我们的单选框属性设置里面:

setSingleChoiceItems;

2、复选框:

 

 复选框就是用到了对话框的设置复选框方法,简单就和单选框设置一样:

 //2.1 编写复选框
    public void btn_multipy2(View view) {
        //2.2 定义一个爱好数组
        String[] hobby = new String[]{"玩手机","玩游戏","睡觉","码代码"};
        //2.3 定义一个默认值数据
        boolean[] booleans = new boolean[]{true,true,false,false};
        Dialog dialog  = new AlertDialog.Builder(this).
                setIcon(R.drawable.icon2).
                setTitle("你的爱好有:").
                setMultiChoiceItems(hobby, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        booleans[i] = b;
                    }
                }).
                setCancelable(false).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //2.4 创建一个可变长度的字符串
                        StringBuffer str = new StringBuffer();
                        str = str.append("你的爱好有:");
                        int j = 0;
                        for (boolean aBoolean : booleans) {
                            if(aBoolean){
                                str = str.append(hobby[j++]+" ");
                            }
                        }

                        //2.5 将结果吐司出去
                        Toast.makeText(Mydialog2Activity.this, str, Toast.LENGTH_SHORT).show();

                    }
                }).
                setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();
        dialog.show();
    }

这里用到了两个数组,一个是存储我们要显示的列表,一个就是现实的状态,如果被选中,这个值对应的状态就位true,那么我们在吐司的时候只需要判断对应的数组的布尔值就可以进行输出了;

那么我们选择的是多个值,要输出的话我们可以使用一个可变长度的字符串,就是我们的缓冲区字符串,使用append方法可以实现选择的数据的追加;

3、登录框:

 同样的,我们使用的是对话框的view方法,可以设置为一个布局,我们现在layout下面创建一个布局文件,也就是要显示到对话框上面的布局,当然你们可以依据自己爱好来选择:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:id="@+id/et_username2"
        android:layout_gravity="center_horizontal"/>
    <EditText
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/et_password2"
        android:inputType="textPassword"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

还记得之前我们制作spinner的时候也用到了自定义布局文件吗:

//3.1 编写登录框
    public void btn_login2(View view) {
        //3.2 获取视图对象
        view = LayoutInflater.from(this).inflate(R.layout.myloginlayout2,null);
        EditText et_username2,et_password2;
        et_username2 = view.findViewById(R.id.et_username2);
        et_password2 = view.findViewById(R.id.et_password2);
        //3.3 创建对话框
        Dialog dialog = new AlertDialog.Builder(this).
                setIcon(R.drawable.icon2).
                setTitle("登录").
                setView(view).
                setCancelable(false).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Mydialog2Activity.this, "用户名:"+et_username2.getText().toString()+
                                "\n密码:"+et_password2.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                }).
                setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).create();
        dialog.show();
    }

我们通过这段代码来获取到我们自定义的布局文件:

 view = LayoutInflater.from(this).inflate(R.layout.myloginlayout2,null);

然后获取到里面的组件对象,通过gettext()方法来拿到对应的字符串,通过toast显示出来,其他部分都是一样的,虽然很多部分和上一期的没什么差别,但是过一段时间会忘记,所以在做一遍巩固一下;

那么,这一期就到这,拜拜

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程学渣ズ

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值