Dialog对话框

一、常用对话框

1.普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        builder.setMessage("内容");
        builder.setIcon(R.mipmap.ic_launcher);//图片
        builder.setCancelable(false);//点击别处是否取消显示
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
2.单选对话框
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 dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
3.多选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        final String[] s=new String[]{"篮球","羽毛球","足球"};
        final boolean[] b=new boolean[]{false,false,false};
        builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < b.length ; i++) {
                    if (b[i]){
                        Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
4.自定义对话框(******)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        final View inflate = LayoutInflater.from(this).inflate(R.layout.alert_layout, null);
        builder.setView(inflate);
        ImageView viewById = inflate.findViewById(R.id.image1);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        final AlertDialog alertDialog = builder.create();
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
<?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">
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30sp"
        android:text="一张图片"></TextView>
    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:src="@mipmap/youhua"
        android:layout_height="wrap_content"></ImageView>

</LinearLayout>

5.水平进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("标题");
        progressDialog.setMax(100);
        progressDialog.setProgress(10);
        progressDialog.setSecondaryProgress(20);
        progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index=0;
            @Override
            public void run() {
                index+=10;
                progressDialog.setProgress(index);
                if (index>=100){
                    progressDialog.cancel();
                    progressDialog.dismiss();
                }
            }
        },0,1000);
        progressDialog.show();
6.圆形进度条对话框
7.日期选择对话框
Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.YEAR);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+"年"+(month+1)+"月"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
8.时间选择对话框
Calendar calendar = Calendar.getInstance();
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true);
        timePickerDialog.show();

二、自定义对话框

1.按钮

//创建类继承Dialog
1.声明接口

public interface YesOnclick{
        void click();
    }

2.声明成员变量

    private YesOnclick yesOnclick;

3.设置->给接口赋值

    public void setYesOnclick(YesOnclick yesOnclick) {
        this.yesOnclick = yesOnclick;
    }

完整步骤

public interface YesOnclick{
        void click();
    }

    private YesOnclick yesOnclick;

    public void setYesOnclick(YesOnclick yesOnclick) {
        this.yesOnclick = yesOnclick;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        initView();
        mes1.setText(strMes);
        title1.setText(strti);
        queding1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                yesOnclick.click();
            }
        });
        quxiao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                noOnclick.click();
            }
        });
    }

调用

MyDialog myDialog = new MyDialog(this);
        myDialog.setStrti("标题45564546564");
        myDialog.setStrMes("内容123456789");
        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();
1.文本

1.findById

 title1 = (TextView) findViewById(R.id.title1);

2.成员变量

private String strti;

3.设置set方法

    public void setStrti(String strti) {
        this.strti = strti;
    }

4.绑定

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        initView();
   	    title1.setText(strti);
    }

5.调用

MyDialog myDialog = new MyDialog(this);
        myDialog.setStrti("标题45564546564");
        myDialog.setStrMes("内容123456789");
        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();

三、案例

主页面XML

<?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="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button1"
        android:text="普通按钮"></Button>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button2"
        android:text="单选按钮"></Button>
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button3"
        android:text="多选按钮"></Button>
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button4"
        android:text="自定义内容按钮"></Button>
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button5"
        android:text="水平计时器按钮"></Button>
    <Button
        android:id="@+id/btn6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button6"
        android:text="日期对话框"></Button>
    <Button
        android:id="@+id/btn7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button7"
        android:text="时间对话框"></Button>
    <Button
        android:id="@+id/btn8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button8"
        android:text="完全自定义对话框"></Button>

</LinearLayout>

自定义XML

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

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundd"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#95D2FD"
            android:text="警告"></TextView>
        <TextView
            android:id="@+id/mes1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:text="保护视力,少玩手机"></TextView>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#E4E4E4">
        </View>
        <LinearLayout
            android:layout_width="300dp"
            android:layout_marginTop="10dp"
            android:layout_height="80dp">
        <Button
            android:id="@+id/quxiao1"
            android:layout_width="120dp"
            android:layout_height="70dp"
            android:layout_marginLeft="30dp"
            android:background="#ffffff"
            android:text="取消"></Button>
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#E4E4E4">
            </View>
        <Button
            android:id="@+id/queding1"
            android:layout_width="120dp"
            android:layout_height="70dp"
            android:background="#ffffff"
            android:textColor="#95D2FD"
            android:text="确定"></Button>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

主页面弹框

package com.example.day1;

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

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class
MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void button1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        builder.setMessage("内容");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    public void button2(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 dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void button3(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        final String[] s=new String[]{"篮球","羽毛球","足球"};
        final boolean[] b=new boolean[]{false,false,false};
        builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < b.length ; i++) {
                    if (b[i]){
                        Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void button4(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        
        final View inflate = LayoutInflater.from(this).inflate(R.layout.alert_layout, null);
        builder.setView(inflate);
        ImageView viewById = inflate.findViewById(R.id.image1);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //builder.show();
        final AlertDialog alertDialog = builder.create();
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();

    }

    public void button5(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("标题");
        progressDialog.setMax(100);
        progressDialog.setProgress(10);
        progressDialog.setSecondaryProgress(20);
        progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index=0;
            @Override
            public void run() {
                index+=10;
                progressDialog.setProgress(index);
                if (index>=100){
                    progressDialog.cancel();
                    progressDialog.dismiss();
                }
            }
        },0,1000);
        progressDialog.show();
    }

    public void button6(View view) {
        Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.YEAR);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+"年"+(month+1)+"月"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    public void button7(View view) {
        Calendar calendar = Calendar.getInstance();
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true);
        timePickerDialog.show();
    }

    public void button8(View view) {
        MyDialog myDialog = new MyDialog(this);
        myDialog.setStrti("标题45564546564");
        myDialog.setStrMes("内容123456789");
        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();
    }
}

自定义弹框

package com.example.day1;

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

import androidx.annotation.NonNull;

public class MyDialog extends Dialog {
    private TextView title1;
    private TextView mes1;
    private Button quxiao1;
    private Button queding1;

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

    private String strMes;

    public void setStrMes(String strMes) {
        this.strMes = strMes;
    }

    private String strti;

    public void setStrti(String strti) {
        this.strti = strti;
    }

    public interface YesOnclick{
        void click();
    }

    private YesOnclick yesOnclick;

    public void setYesOnclick(YesOnclick yesOnclick) {
        this.yesOnclick = yesOnclick;
    }

    public interface NoOnclick{
        void click();
    }

    private NoOnclick noOnclick;


    public void setNoOnclick(NoOnclick noOnclick) {
        this.noOnclick = noOnclick;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        initView();
        mes1.setText(strMes);
        title1.setText(strti);
        queding1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                yesOnclick.click();
            }
        });
        quxiao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                noOnclick.click();
            }
        });
    }

    private void initView() {
        title1 = (TextView) findViewById(R.id.title1);
        mes1 = (TextView) findViewById(R.id.mes1);
        quxiao1 = (Button) findViewById(R.id.quxiao1);
        queding1 = (Button) findViewById(R.id.queding1);
    }
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值