常用对话框
1普通对话框
2单选对话框
3多选对话框
4时间对话框
5日期对话框
6进度条对话框
7自定义对话框
package com.example.day0012;
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 {
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setMessage("内容");
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();
}
});
alertDialog = builder.create();
alertDialog.show();
}
public void btn1(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
final String[] s = new String[]{"篮球", "游戏", "开发"};
final boolean[] b = new boolean[]{true, true, false};
builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
}
});
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();
}
});
alertDialog = builder.create();
alertDialog.show();
}
public void btn2(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
View inflate = LayoutInflater.from(this).inflate(R.layout.style1, null);
builder.setView(inflate);
ImageView img = inflate.findViewById(R.id.img);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
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();
}
});
alertDialog = builder.create();
alertDialog.show();
}
public void btn3(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("标题");
progressDialog.setMessage("内容");
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.dismiss();
}
}
}, 0, 1000);
progressDialog.show();
}
public void btn4(View view) {
Calendar calendar = Calendar.getInstance();
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 btn5(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), false);
timePickerDialog.show();
}
public void btn6(View view) {
MyDialog myDialog = new MyDialog(this);
myDialog.setStrMsg("哈哈哈");
myDialog.setStrTi("我是标题");
myDialog.setOnclick(new MyDialog.onclick() {
@Override
public void click() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
myDialog.setOnyes(new MyDialog.onyes() {
@Override
public void noyes() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
myDialog.show();
}
}
自定义类 自定义对话框
package com.example.day0012;
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 ti;
private TextView message;
private Button no;
private Button yes;
private String strMsg;
private String strTi;
public interface onyes{
void noyes();
}
private onyes onyes;
public void setOnyes(MyDialog.onyes onyes) {
this.onyes = onyes;
}
public interface onclick{
void click();
}
private onclick onclick;
public void setOnclick(MyDialog.onclick onclick) {
this.onclick = onclick;
}
public void setStrTi(String strTi) {
this.strTi = strTi;
}
public void setStrMsg(String strMsg) {
this.strMsg = strMsg;
}
public MyDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.style2);
ti = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
message.setText(strMsg);
ti.setText(strTi);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
onyes.noyes();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
onclick.click();
}
});
}
}
自定义布局
<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>