对话框 | 用法 |
---|---|
普通 | 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() |
private void SimpleDialog() {
btn1 = (Button) findViewById(R.id.btn_1);
btn1.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.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 alertDialog = builder.create();
//显示对话框
alertDialog.show();
}
});
}
private void SelectDialog() {
btn2 = (Button) findViewById(R.id.btn_2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("单选框");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确认", Toast.LENGTH_SHORT).show();
}
});
final String[] str = {"java", "php", "python"};
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 MuliteDialog() {
btn3 = findViewById(R.id.btn_3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//初始化数据
final String[] str = {"java", "php", "python"};
final boolean[] booleans = {false, false, false};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("复选框");
builder.setMultiChoiceItems(str, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
booleans[which] = isChecked;
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < booleans.length; i++) {
if (booleans[i]) {
int count = i + 1;
Toast.makeText(MainActivity.this, "勾选" + count + "项", Toast.LENGTH_SHORT).show();
}
}
}
});
//创建对话框
AlertDialog alertDialog = builder.create();
//显示对话框
alertDialog.show();
}
});
}
private void CustomMsgDialog() {
btn4 = findViewById(R.id.btn_4);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("自定义对话框");
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.customer_dialog, null);
final TextView tv_msg = view.findViewById(R.id.tv_msg);
tv_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + tv_msg.getText(), Toast.LENGTH_SHORT).show();
}
});
//将视图插入到对话框中
builder.setView(view);
//确定按钮
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
private void PlaneProgressDialog() {
btn5 = findViewById(R.id.btn_5);
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("正在下载...");
//设置风格
//水平
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//模糊(圆圈)
// progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMax(100);
progressDialog.show();
//模拟定时器
final Timer timer = new Timer();
/**
* 三个参数
* 1.TimerTask任务
* 2.几秒后执行
* 3.每隔几秒执行一次
*/
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress == 100) {
timer.cancel();//取消定时器
progressDialog.dismiss();//消失
}
progressDialog.setProgress(progress++);
}
}, 1, 100);//1秒后执行,每隔100毫秒执行一次
}
});
}
代码如下:
private void DataDialog() {
/**
*
*
* @param context 上下文
* @param listener 监听器
* @param year 默认的年
* @param month 默认的月
* @param dayOfMonth 默认的日
*/
btn6 = findViewById(R.id.btn_6);
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取日历对象
final Calendar calendar = 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+"年"+(month+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void TimeDialog() {
btn7 = findViewById(R.id.btn_7);
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
new TimePickerDialog(MainActivity.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),true).show();//isHourView是否采用24小时进制
}
});
}
首先需要用户自定义对话框,上布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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>
有了布局文件,接下来就是要创建类,来设置具体怎么使用
public class UserDefinedDialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
//确定文本和取消文本的显示的内容
private String yesStr, noStr;
//取消按钮被点击了的监听器
private onNoOnclickListener noOnclickListener;
//确定按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;
public UserDefinedDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdefined_dialog);
//初始化控件
initViews();
//初始化数据
initDatas();
//初始化控件事件
initEvents();
}
private void initEvents() {
//设置确定按钮被点击后,向外界提供监听
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();
}
}
});
}
private void initDatas() {
//做非空判断,防止数据为空
if (titleStr!=null){
title.setText(titleStr);
}
if (messageStr!=null){
message.setText(messageStr);
}
if (yesStr!=null){
yes.setText(yesStr);
}
if (noStr!=null){
no.setText(noStr);
}
}
private void initViews() {
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
}
//从外界Activity为UserDefinedDialog设置标题
public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}
public void setMessageStr(String messageStr) {
this.messageStr = messageStr;
}
//自定义点击事件
public interface onNoOnclickListener {
public void onNoClick();
}
public interface onYesOnclickListener {
public void onYesOnclick();
}
public void setNoOnclickListener(String str,onNoOnclickListener noOnclickListener) {
if (str!=null){
noStr = str;
}
this.noOnclickListener = noOnclickListener;
}
public void setYesOnclickListener(String str,onYesOnclickListener yesOnclickListener) {
if (str!=null){
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
}
最后在MainActivity中将自定义的对话框进行创建,添加内容即可
private void UserDefinedDialog() {
btn8 = findViewById(R.id.btn_8);
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final UserDefinedDialog dialog = new UserDefinedDialog(MainActivity.this);
//通过自己设置的方法,跟新对话框内容
dialog.setTitleStr("这是title");
dialog.setMessageStr("这是提示信息");
//确定按钮
dialog.setYesOnclickListener("yes", new UserDefinedDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
Toast.makeText(MainActivity.this, "按钮被点击", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
//取消按钮
dialog.setNoOnclickListener("no", new UserDefinedDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivity.this, "按钮被点击", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
//显示对话框
dialog.show();
}
});
}
最终效果: