Dialog对话框

一、常用对话框

对话框特殊
普通AlertDialog.Builder()
单选AlertDialog.Builder()setSingleChoiceItems()
多选AlertDialog.Builder()setMultiChoiceItems()
日期DatePickerDialognew DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day);
时间TimePickerDialognew TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制);
水平ProgressDialogsetStyle(ProgressDialog.STYLE_HORIZONTAL)
圆圈ProgressDialogsetStyle(ProgressDialog.STYLE_SPINNER)
自定义AlertDialog.Builder()setView()

1.普通对话框

在这里插入图片描述

    public void btn(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
        builder.setMessage("内容");
        builder.setIcon(R.mipmap.b1);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

2.单选对话框

在这里插入图片描述

 public void danxuan(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");

        final String[] s =new String[]{"学习","干饭","游戏"};
        builder.setSingleChoiceItems(s, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

3.多选对话框

在这里插入图片描述

 public void duoxuan(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("多选框");

        final String[] s = new String[]{"学习","干饭","游戏"};
        final boolean[] boo = new boolean[]{true,true,false};

        builder.setMultiChoiceItems(s, boo, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                if(b){
                    Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                }
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                for (int j = 0; j <boo.length ; j++) {
                    if(boo[j]){
                        Toast.makeText(MainActivity.this, s[j], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();


    }

4.水平进度条对话框

在这里插入图片描述

 public void shuiping(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("水平进度条");
        progressDialog.setMax(100);
        progressDialog.setProgress(10);//进度值
        progressDialog.setSecondaryProgress(20);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


        //定时器
        final Timer timer = new Timer();//闹钟
        progressDialog.show();
        timer.schedule(new TimerTask() {
            int index=0;

            @Override
            public void run() {
                index+= 10;
                progressDialog.setProgress(index);
                if(index>=100){

                    progressDialog.dismiss();
                    timer.cancel();
                }
            }
            //0:  首次延迟的时间
        },0,1000);

        progressDialog.show();
    }

5.圆形进度条对话框

在这里插入图片描述

private void hor_progress_dialog() {
        //TODO 1:实例化对象
        final ProgressDialog dialog=new ProgressDialog(this);
        //TODO 2:设置属性
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置圆圈
        dialog.setMax(100);
        dialog.setMessage("正在下载");
        //TODO 3:显示
        dialog.show();
        //TODO 4:模拟定时器
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                if(progress==100){
                    dialog.dismiss();//消失
                    timer.cancel();
                }
                dialog.setProgress(progress+=20);
            }
        },0,1000);//0秒后执行,每隔1000毫秒执行一次
    }

6.日期选择对话框

在这里插入图片描述

    public void riqi(View view) {
        Calendar calendar = Calendar.getInstance();

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            /**
             *
             * @param datePicker
             * @param i
             * @param //i1从0到11
             * @param i2
             */
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                Toast.makeText(MainActivity.this, i+"年"+(i1+1)+"月"+i2, Toast.LENGTH_SHORT).show();

            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
        
    }

7.时间选择对话框

在这里插入图片描述

    public void shijian(View view) {
        Calendar instance = Calendar.getInstance();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                Toast.makeText(MainActivity.this, i+":"+i1, Toast.LENGTH_SHORT).show();
            }
        },instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();

    }

8.图片对话框

在这里插入图片描述

 public void tupian(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("图片");
        View inflate = LayoutInflater.from(this).inflate(R.layout.activity_main2, null);
        builder.setView(inflate);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        final AlertDialog alertDialog = builder.create();
        ImageView imageView = inflate.findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });

        alertDialog.show();


    }

二、自定义对话框

代码&效果

在这里插入图片描述

//java main里的代码
 public void zidingyi(View view) {

        MyDialog myDialog = new MyDialog(this);

        //调用
        myDialog.setStrMsg("哈哈哈哈h哈哈哈哈哈");
        myDialog.setStrTi("标题吞吞吐吐拖拖拖");
      //  myDialog.show();

        myDialog.setNoOnClick(new MyDialog.NoOnClick() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.setYesOnClick(new MyDialog.YesOnClick() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });


        myDialog.show();
    }
//自定义MyDialog 类里代码
package com.example.a1810d1;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import javax.security.auth.login.LoginException;

public class MyDialog extends Dialog {
    private static final String TAG = "MyDialog";
    private TextView ti;
    private TextView message;
    private Button no;
    private Button yes;

    public MyDialog(@NonNull Context context) {
        super(context);
    }

    private String strMsg;
    public void setStrMsg(String strMsg) {
        this.strMsg = strMsg;
    }

    private  String strTi;
    public void setStrTi(String strTi) {
        this.strTi = strTi;
    }

    //1.声明接口
    public interface NoOnClick{
        void click();
    }

    public interface YesOnClick{
        void click();
    }
    //2.声明成员变量
    private NoOnClick noOnClick;
    private YesOnClick yesOnClick;

    //3.设置->给接口赋值
    public void setNoOnClick(NoOnClick noOnClick){
        this.noOnClick = noOnClick;
    }

    public void setYesOnClick(YesOnClick yesOnClick) {
        this.yesOnClick = yesOnClick;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_dialog);
        initView();
        //绑定
        message.setText(strMsg);
       ti.setText(strTi);
        //点击事件
       no.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Log.e(TAG, "onClick: ");
               dismiss();
               noOnClick.click();
           }
       });
       yes.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Log.e(TAG, "onClick1: " );
               dismiss();
               yesOnClick.click();
           }
       });
    }
    private void initView() {
        ti = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);
    }
}
<?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"
    tools:context=".MyDialog">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#11ffffff">
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="警告"

                android:textColor="#38ADFF"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:layout_gravity="center"
                android:text="保护视力,少玩手机"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:layout_marginTop="15dp"
                android:background="#E4E4E4"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/no"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_marginLeft="10dp"
                    android:background="@null"
                    android:gravity="center"
                    android:lines="1"
                    android:text="取消"
                    android:textColor="#7D7D7D"
                    android:textSize="16sp"/>
                <View
                    android:layout_width="1px"
                    android:layout_height="match_parent"
                    android:background="#E4E4E4"/>
                <Button
                    android:id="@+id/yes"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_marginRight="10dp"
                    android:background="@null"
                    android:gravity="center"
                    android:lines="1"
                    android:text="确定"
                    android:textColor="#38ADFF"
                    android:textSize="16sp"/>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值