对话框

折月煮酒 提交于 2019-12-27 01:07:11
package com.example.myapplication1;

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.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

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

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

public class MainActivity extends AppCompatActivity {
    private Button normal;
    private Button single;
    private Button duo;
    private Button pro;
    private Button pro2;
    private Button me;
    private Button date;
    private Button tim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        normal = (Button) findViewById(R.id.normal);
        single = (Button) findViewById(R.id.single);
        duo = (Button) findViewById(R.id.duo);
        pro = (Button) findViewById(R.id.pro);
        pro2 = (Button) findViewById(R.id.pro2);
        me = (Button) findViewById(R.id.me);
        date = (Button) findViewById(R.id.date);
        tim = (Button) findViewById(R.id.tim);
        normal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                normal();
            }
        });
        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                single();
            }
        });
        duo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mulite();
            }
        });
        pro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hor_progress();
            }
        });
        pro2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customer();
            }
        });
        me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customer();
            }
        });
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                date();
            }
        });
        tim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time();
            }
        });

    }

    //普通对话框
    public void normal() {
        //构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置头像
        builder.setIcon(R.drawable.ic_launcher_background);
        //设置标题
        builder.setTitle("你好!");
        //设置内容
        builder.setMessage("这里是1711A开发");
        //设置确定按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击的OK", Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击的NO", Toast.LENGTH_SHORT).show();
            }
        });
        //使用构建者创建对话框
        AlertDialog alertDialog = builder.create();
        //设置显示
        alertDialog.show();
    }

    //单选对话框
    private void single() {
        //创建构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置标题
        builder.setTitle("世界上的性别");
        //设置图标
        builder.setIcon(R.mipmap.ic_launcher);
        //设置确定按钮
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击OK", Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击NO", Toast.LENGTH_SHORT).show();
            }
        });
        //设置单选需要的数据
        final String[] str = {"男", "女", "人妖"};
        //填充数据 参数一 :参数来源 参数二:默认选中的下标 参数三:设置点击监听事件
        builder.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击" + str[which], Toast.LENGTH_SHORT).show();
            }
        });
        //使用创建者创建对话框
        AlertDialog alertDialog = builder.create();
        //显示对话框
        alertDialog.show();
    }

    private void mulite() {
        final String[] str = {"张三", "李四", "王五", "赵六"};
        final boolean[] flag = {false, false, false, false};
        //创建构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setTitle("朋友");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (int i = 0; i < str.length; i++) {
                    if (flag[i]) {
                        Toast.makeText(MainActivity.this, "是" + str[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了NO", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setMultiChoiceItems(str, flag, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                flag[which] = isChecked;
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void customer() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.me_layout, null);
        ImageView viewById = inflate.findViewById(R.id.img);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "您点击了我", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "您点击了确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "您点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setView(inflate);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void hor_progress() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置精准样式
        progressDialog.setMax(100);//设置最大进度
        progressDialog.setMessage("正在下载");
        progressDialog.show();
        //使用计时器模拟进度
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;

            @Override
            public void run() {
                //当当前进度等于最大进度是停止
                if (progress == progressDialog.getMax()) {
                    progressDialog.dismiss();
                    progressDialog.cancel();
                }
                progressDialog.setProgress(progress += 10);
            }
        }, 0, 1000);
    }

    private void progress() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置模糊样式
        progressDialog.setMax(100);//设置最大进度
        progressDialog.setMessage("正在下载");
        progressDialog.show();
        //使用计时器模拟进度
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;

            @Override
            public void run() {
                //当当前进度等于最大进度是停止
                if (progress == progressDialog.getMax()) {
                    progressDialog.dismiss();
                    progressDialog.cancel();
                }
                progressDialog.setProgress(progress += 10);
            }
        }, 0, 1000);
    }

    private void date() {

        Calendar instance = Calendar.getInstance();//生成日历对象
        /*
         * @param context 上下文
         * @param listener 监听器
         * @param year 默认的年
         * @param month 默认的月
         * @param dayOfMonth 默认的日
         */
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year + "-" + month + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void time() {
        Calendar instance = Calendar.getInstance();
        /*
         * @param context 上下文
         * @param listener 监听器
         * @param hourOfDay 默认的时
         * @param minute 默认的分钟
         * @param is24HourView 是否采用24时
         */
        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();
            }
        }, instance.get(Calendar.HOUR), instance.get(Calendar.MINUTE), true).show();
    }


}
<?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/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通对话框" />

    <Button
        android:id="@+id/single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选对话框" />

    <Button
        android:id="@+id/duo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选对话框" />

    <Button
        android:id="@+id/pro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进度条(精准)对话框" />

    <Button
        android:id="@+id/pro2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进度条(模糊)对话框" />

    <Button
        android:id="@+id/me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义对话框" />

    <Button
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期对话框" />

    <Button
        android:id="@+id/tim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间对话框" />

</LinearLayout>
<?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:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_foreground" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大美女"
        android:textSize="30sp" />
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!