小白开发程序之路(3-2)AlertDialog

代码

activity_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">
    <Button
        android:id="@+id/btn_dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="style1"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_dialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="style2"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_dialog3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="style3"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_dialog4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="style4"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/btn_dialog5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="style5"
        android:textAllCaps="false"/>
</LinearLayout>

DiaglogActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.util.ToastUtil;

public class DialogActivity extends AppCompatActivity {
    private Button mBtnDialog1,mBtnDialog2,mBtnDialog3,mBtnDialog4,mBtnDialog5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        mBtnDialog1=findViewById(R.id.btn_dialog1);
        mBtnDialog2=findViewById(R.id.btn_dialog2);
        mBtnDialog3=findViewById(R.id.btn_dialog3);
        mBtnDialog4=findViewById(R.id.btn_dialog4);
        mBtnDialog5=findViewById(R.id.btn_dialog5);
        Onclick onclick=new Onclick();
        mBtnDialog1.setOnClickListener(onclick);
        mBtnDialog2.setOnClickListener(onclick);
        mBtnDialog3.setOnClickListener(onclick);
        mBtnDialog4.setOnClickListener(onclick);
        mBtnDialog5.setOnClickListener(onclick);
    }
    class Onclick implements View.OnClickListener {
        @Override
        public void onClick(View view) {

            switch(view.getId()){
                case R.id.btn_dialog1:
                    LayoutInflater inflater=LayoutInflater.from(DialogActivity.this);
                    View viewCustom = inflater.inflate(R.layout.layout_list_item,null);
                    ImageView imageView=viewCustom.findViewById(R.id.iv);
                    TextView textView1=viewCustom.findViewById(R.id.tv_title2);
                    TextView textView2=viewCustom.findViewById(R.id.tv_time);
                    TextView textView3=viewCustom.findViewById(R.id.tv_content);
                    Glide.with(getApplicationContext()).load("https://i0.hdslb.com/bfs/article/1f2d43c9129520f5e595ab7d5b7eae8fe64b009d.jpg@1320w_778h.webp").into(imageView);
                    textView1.setText("若爱有颜色");
                    textView2.setText("那一定是");
                    textView3.setText("蓝色");
                    //上面是尝试了builder的setView方法,用的是之前的layout_list_item,注意如果想利用布局文件生成view的话是需要借助inflater的
                    AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
                    //新版本的这里选androidx下的ALertDialog就可以了
                    builder.setTitle("请回答").setMessage("你觉得蕾姆如何?").setView(viewCustom).setIcon(R.drawable.icon_user)
                            .setPositiveButton("好", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMsg(DialogActivity.this,"你在想peach,蕾姆是我的");
                                }
                            }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"好好说话");
                        }
                    }).setNegativeButton("不太行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"闭嘴吧你");

                        }
                    }).show();
                    //setMessage 设置文本内容,setIcon 设置图标,setPositiveButton设置肯定选项,setNegativiButton设置否定选项 ,setNeutralButton设置中性选项
                    //注意到setTitle返回的就是一个builder,所以setMessage可以直接跟在setTitle后面,setPositiveButton、setNegativeButton、setNeutralButton同理
                    //最后记得调用show方法
                    break;
                case R.id.btn_dialog2:
                    final String[]array2=new String[]{"男","女"};
                    AlertDialog.Builder builder2=new AlertDialog.Builder(DialogActivity.this);
                    builder2.setTitle("选择性别").setItems(array2, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,array2[i]);
                        }
                    }).show();
                    break;
                case R.id.btn_dialog3:
                    final String[]array3=new String[]{"男","女"};
                    AlertDialog.Builder builder3=new AlertDialog.Builder(DialogActivity.this);
                    builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
                        //第二个参数checkeditem指的是默认选择
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //int i的参数指的是点击的是第几个选项
                            ToastUtil.showMsg(DialogActivity.this,array3[i]);
                            dialogInterface.dismiss();
                            //弹出Toast后对话框消失
                        }
                    }).setCancelable(false).show();
                    //setCancelable(false)指的是点击对话框以外的位置对话框不会消失
                    break;
                case R.id.btn_dialog4:
                    final String[]array4=new String[]{"唱歌","跳舞","写代码"};
                    boolean[] isSelected =new boolean[]{false,false,true};
                    AlertDialog.Builder builder4=new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("选择兴趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                        //这里传入的isSelected是默认选中情况
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                            //i是第几个,b是是否被选中
                            ToastUtil.showMsg(DialogActivity.this,array4[i]+":"+b);
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        //加个确定按钮
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        //再加个取消按钮
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).show();

                    break;
                case R.id.btn_dialog5:
                    final AlertDialog.Builder builder5=new AlertDialog.Builder(DialogActivity.this);
                    View viewcustom=LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
                    EditText etUserName=viewcustom.findViewById(R.id.et_username);
                    EditText etPassWord=viewcustom.findViewById(R.id.et_password);
                    Button btnLogin=viewcustom.findViewById(R.id.btn_login);
                    btnLogin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ToastUtil.showMsg(DialogActivity.this,"正在登录中...");
                    }});
                    builder5.setTitle("请先登录").setView(viewcustom).show();
                    break;

            }
        }
    }
}

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:padding="15dp"
    android:orientation="vertical">
    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="username"
        android:maxLines="1"/>
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="password"
        android:maxLines="1"
        android:layout_marginTop="10dp"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="login"
        android:layout_marginTop="10dp"
        android:textAllCaps="false"/>


</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值