Activity_main.xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义布局view对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:textAllCaps="false"
android:textSize="28sp"/>
</LinearLayout>
MainActivity.java
package com.example.hyl_alterdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
//TextView edit_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1=findViewById(R.id.button1);
btn_2=findViewById(R.id.button2);
btn_3=findViewById(R.id.button3);
btn_4=findViewById(R.id.button4);
btn_5=findViewById(R.id.button5);
btn_6=findViewById(R.id.button6);
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("温馨提示");
builder.setMessage("确定要退出吗?");
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,int which) {
finish();
}
});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,int which) {
finish();
}
});
builder.create().show();
}
});
btn_2.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("请选择城市");
final String[] cities = {"北京", "上海", "广州", "深圳"};
// 初始选中项的位置
int checkedItem = 0;
// 初始化TextView用于显示选中的城市
TextView selectedCity = new TextView(MainActivity.this);
selectedCity.setText("请选择城市");
builder.setCustomTitle(selectedCity);
builder.setSingleChoiceItems(cities, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
// 更新TextView显示选中的城市
selectedCity.setText(cities[which]);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
// 在这里处理确定按钮的逻辑操作
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
// 在这里处理取消按钮的逻辑操作
}
});
builder.create().show();
}
});
btn_3.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("选择喜欢的水果");
// 初始化复选框状态和选项
final boolean[] checkedItems = {false, false, false, false};
final String[] items = {"香蕉", "苹果", "橙子", "葡萄"};
// 设置多选项
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
// 设置确定按钮的点击事件
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
result.append(items[i]).append(" ");
}
}
Toast.makeText(MainActivity.this, "你选择了:" + result.toString(), Toast.LENGTH_SHORT).show();
}
});
// 设置取消按钮的点击事件
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 这里可以添加取消按钮的逻辑操作
}
});
// 创建对话框并显示
builder.create().show();
}
});
btn_4.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.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
builder.create().show();
}
});
btn_5.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("列表对话框");
final String[] options = {"北京", "上海", "广州", "深圳"};
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
}
});
builder.create().show();
}
});
btn_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setIcon(R.mipmap.ic_launcher_round);
progressDialog.setTitle("处理中");
progressDialog.setMessage("请稍候...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 或者使用ProgressDialog.STYLE_HORIZONTAL,显示水平进度条
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
// 模拟耗时操作
try {
Thread.sleep(3000); // 3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
progressDialog.dismiss(); // 关闭对话框
}
}).start();
}
});
}
}