一、常用对话框
对话框 | 类 | 特殊 |
---|
普通 | AlertDialog.Builder() | |
单选 | AlertDialog.Builder() | setSingleChoiceItems() |
多选 | AlertDialog.Builder() | setMultiChoiceItems() |
日期 | DatePickerDialog | new DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day); |
时间 | TimePickerDialog | new TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制); |
水平 | ProgressDialog | setStyle(ProgressDialog.STYLE_HORIZONTAL) |
圆圈 | ProgressDialog | setStyle(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,1000);
progressDialog.show();
}
5.圆形进度条对话框
private void hor_progress_dialog() {
final ProgressDialog dialog=new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
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);
}
},0,1000);
}
6.日期选择对话框
public void riqi(View view) {
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@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();
}
二、自定义对话框
代码&效果
public void zidingyi(View view) {
MyDialog myDialog = new MyDialog(this);
myDialog.setStrMsg("哈哈哈哈h哈哈哈哈哈");
myDialog.setStrTi("标题吞吞吐吐拖拖拖");
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.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;
}
public interface NoOnClick{
void click();
}
public interface YesOnClick{
void click();
}
private NoOnClick noOnClick;
private YesOnClick yesOnClick;
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>