布局
<?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=".MainActivity"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Day1.弹出框"
android:textSize="25sp"
android:layout_marginBottom="20dp"
/>
<Button
android:id="@+id/normal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/single_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/multiple_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/progress_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/date_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期选择弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/time_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间选择弹出框"
android:onClick="click"
/>
<Button
android:id="@+id/tcustom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完全自定义弹出框"
android:onClick="click"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="151dp"
>
<TextView
android:id="@+id/head"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:text="警告!"
android:textColor="#F70202"
android:textSize="20dp"
android:layout_margin="15dp"
/>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="少玩手机,保护视力"
android:textColor="#2196F3"
android:gravity="center"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c3c3c3"
></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消"
android:gravity="center"
android:textColor="@drawable/choose"
/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#c3c3c3"
>
</View>
<TextView
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确认"
android:gravity="center"
android:textColor="@drawable/choose"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/hello"
/>
</LinearLayout>
外部类Dialog
package com.example.myapplication;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MyDialog extends Dialog {
private TextView head,msg,no,yes;
String headtext;
String message;
String notext;
String yestext;
public void setHeadtext(String headtext) {
this.headtext = headtext;
}
public void setMessage(String message) {
this.message = message;
}
public void setNotext(String notext) {
this.notext = notext;
}
public void setYestext(String yestext) {
this.yestext = yestext;
}
@Override
public String toString() {
return "MyDialog{" +
"head=" + head +
", msg=" + msg +
", no=" + no +
", yes=" + yes +
'}';
}
public MyDialog(Context context) {
super(context);
}
private Yesclick yesclick;
private Noclick noclick;
public interface Yesclick{
void onClick();
}
public interface Noclick{
void onClick();
}
public void setNoclick(Noclick noclick) {
this.noclick = noclick;
}
public void setYesclick(Yesclick yesclick) {
this.yesclick = yesclick;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview2);
head = (TextView) findViewById(R.id.head);
msg = (TextView) findViewById(R.id.msg);
no = (TextView) findViewById(R.id.no);
yes = (TextView) findViewById(R.id.yes);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yesclick.onClick();
dismiss();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
noclick.onClick();
dismiss();
}
});
if(headtext!=null){
head.setText(headtext);
}
if(message!=null){
msg.setText(message);
}
if (yestext!=null){
yes.setText(yestext);
}
if (notext!=null){
no.setText(notext);
}
}
}
Activity:
package com.example.myapplication;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private Button normalButton;
private Button singleButton;
private Button multipleButton;
private Button customButton;
private Button progressButton;
private Button tcustomButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tcustomButton = (Button) findViewById(R.id.tcustom_button);
normalButton = (Button) findViewById(R.id.normal_button);
singleButton = (Button) findViewById(R.id.single_button);
multipleButton = (Button) findViewById(R.id.multiple_button);
customButton = (Button) findViewById(R.id.custom_button);
progressButton = (Button) findViewById(R.id.progress_button);
}
private void chance() {
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//属性
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setTitle("八维目标薪资同统计:");//设置标题
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("CENCAL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了CENCAL",Toast.LENGTH_SHORT).show();
}
});
final String[] items={"10~15K","15~20K","20K+"};
//设置单选列表
//参数一 ,列表 参数二,默认选中下表 参数三,事件监听
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你的目标薪资是"+items[which], Toast.LENGTH_SHORT).show();
}
});
//建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
private void normal(){
//创建构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.mipmap.ic_launcher);//图标
builder.setTitle("请确定你的选择");//标题
builder.setMessage("是否高薪就业?");//内容
//确定按钮
builder.setPositiveButton("YES", 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();
}
});
//使用建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
private void custom(){
View view= LayoutInflater.from(this).inflate(R.layout.myview,null);
ImageView imageView=view.findViewById(R.id.pic);
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置自定义布局
builder.setView(view);
//建造者创建对话框
final AlertDialog dialog = builder.create();
//显示
dialog.show();
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
private void progress() {
final ProgressDialog dialog=new ProgressDialog(this);
//属性
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条样式为水平
dialog.setMax(100);//最大进度值
dialog.setMessage("正在下载");
//显示
dialog.show();
//定时器
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);//步长值设为20
}
},0,1000);//0秒后执行,每隔1000毫秒执行一次
}
private void multiple() {
final String[] items={"唱","跳","Rap","篮球"};
final boolean[] flags={true,true,true,false};
//构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//画画
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("蔡徐坤的特长");//设置标题
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了CANCEL",Toast.LENGTH_SHORT).show();
}
});
//设置多选列表
//参数一:列表,参数二:默认选中下表,参数三:事件监听
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
}
});
//建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
private void tcustom(){
MyDialog myDialog = new MyDialog(this);
//重新设置各个控件的内容
myDialog.setHeadtext("温馨提示");
myDialog.setMessage("请确认是否系好安全带");
myDialog.setYestext("系好了");
myDialog.setNotext("没系呢");
myDialog.setYesclick(new MyDialog.Yesclick() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
myDialog.setNoclick(new MyDialog.Noclick() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
myDialog.show();
}
public void click(View view) {
switch (view.getId()){
case R.id.normal_button:
normal();
break;
case R.id.single_button:
chance();
break;
case R.id.custom_button:
custom();
break;
case R.id.multiple_button:
multiple();
break;
case R.id.progress_button:
progress();
break;
case R.id.tcustom_button:
tcustom();
break;
}
}
}