单选
//点选对话框
button01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.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("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了 cancel", Toast.LENGTH_SHORT).show();
}
});
final String[] strings = {"北京","上海","广州","深圳"};
builder.setSingleChoiceItems(strings, 3, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了:"+strings[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
多选
//多选
button02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.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("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了: cancel", Toast.LENGTH_SHORT).show();
}
});
final String[] strings = {"赤","橙","红","绿"};
boolean[] booleans = {true,true,true,true};
builder.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, "您点击了:"+strings[which], Toast.LENGTH_SHORT).show();
}
});
自定义布局
//自定义布局
button03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.zidingyi_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(inflate);
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("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "cancel", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
水平
//水平
button04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setMessage("开始下载。。。");
dialog.show();
new CountDownTimer(5000, 50) {
@Override
public void onTick(long millisUntilFinished) {
//增加进度
dialog.incrementProgressBy(1);
}
@Override
public void onFinish() {
//结束
dialog.dismiss();
}
}.start();
}
});
圆形
//圆形
button05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMax(100);
dialog.setMessage("请稍等,开始下载。。。");
dialog.show();
//开启增加方法
new CountDownTimer(5000, 50) {
@Override
public void onTick(long millisUntilFinished) {
//增加进度
dialog.incrementProgressBy(1);
}
@Override
public void onFinish() {
//结束方法
dialog.dismiss();
}
}.start();
}
});
日期
//日期
button06.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
//这是日期类对象
Calendar instance = Calendar.getInstance();
//设置监听
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, ""+year+"\t"+(month+1)+"\t"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
}
});
时间
//时间
button07.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
//这是日期类对象
Calendar instance = Calendar.getInstance();
//设置监听
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, ""+hourOfDay+"\t"+minute, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MONTH),true).show();
}
});
自定义
首先是自定义的布局
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
and roid: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>
继承Dialog
package com.example.day_01_dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.PluralsRes;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
class DiyDialog extends Dialog {
private Button yes;
private Button no;
private TextView titleTV;
private TextView message;
private String titleStr;
private String messageStr;
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;
private onYesOnclickListener yesOnclickListener;
public DiyDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = display.getWidth()*4/5;
getWindow().setAttributes(lp);
}
public DiyDialog(@NonNull Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zidingyi_dialog);
setCanceledOnTouchOutside(false);
initView();
initData();
initEvent();
}
private void initView() {
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
titleTV = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
}
private void initData() {
if (titleStr != null) {
titleTV.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
private void initEvent() {
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick();
}
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
public interface onNoOnclickListener {
public void onNoClick();
}
public interface onYesOnclickListener {
public void onYesOnclick();
}
}
点击监听。。在activity里
//自定义
button08.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new DiyDialog(MainActivity.this);
((DiyDialog) dialog).setTitle("自定义对话框");
((DiyDialog) dialog).setMessage("标题");
((DiyDialog) dialog).setNoOnclickListener("cancel", new DiyDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivity.this, "您点击了: "+"cancel", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
((DiyDialog) dialog).setYesOnclickListener("ok", new DiyDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
Toast.makeText(MainActivity.this, "您点击了: "+"ok", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();
}
});