MainActivity.java
package com.zp.alertdailogbuilderdemo;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//确定取消对话框
public void onclick1(View view){
//拿到对话框的工厂
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置提示内容
builder.setMessage("确定要退出?");
//设置标题
builder.setTitle("提示");
//确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//显示对话框
builder.show();
}
//单选对话框
public void onclick2(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("请选择你的性别");
final String[] items={"男","女"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择的性别是:"+items[which],Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
//多选对话框
public void onclick3(View view){
//拿到对话框的工厂
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置标题
builder.setTitle("请选择你的爱好!");
//选项
final String[] items={"篮球","乒乓球","足球","音乐","美术"};
//多选按钮是否被选中
final boolean[] checked={false,false,false,false,false};
builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checked[which]=isChecked;//改变多选按钮是否被选中中的值
}
});
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.length; i++) {
if (checked[i]) {
stringBuilder.append(items[i]);
}
}
Toast.makeText(MainActivity.this,"你的爱好是:"+stringBuilder.toString(),Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
//进度条对话框
public void onclick4(View view){
final ProgressDialog dialog=new ProgressDialog(this);
dialog.setTitle("请稍后");
dialog.setMessage("正在玩命的加载数据中。。。。");
new Thread(){
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.run();
dialog.dismiss();
}
}.start();
dialog.show();
}
//带进度的进度条
public void onclick5(View view){
final ProgressDialog dialog=new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条dialog样式
dialog.setTitle("正在加载数据");
dialog.setMax(100);//设置进度条的最大进度
//开起子线程防止anr
new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
try {
Thread.sleep(300);//睡眠300ms
dialog.setProgress(i);//设置进度进度
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dialog.dismiss();//关掉进度条
super.run();
}
}.start();
dialog.setCancelable(false);//不让点击其他地方消失dialog
dialog.show();//显示进度条
}
//自定义对话框
public void onclick6(View view){
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
View view1 = getLayoutInflater().inflate(R.layout.dilog, null);
View submit = view1.findViewById(R.id.submit);
View exit = view1.findViewById(R.id.exit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"submit",Toast.LENGTH_SHORT).show();
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"exit",Toast.LENGTH_SHORT).show();
}
});
builder.setView(view1);
builder.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zp.alertdailogbuilderdemo.MainActivity">
<Button
android:onClick="onclick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定取消对话框"/>
<Button
android:onClick="onclick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选对话框"/>
<Button
android:onClick="onclick3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选对话框"/>
<Button
android:onClick="onclick4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"/>
<Button
android:onClick="onclick5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带进度的进度条对话框"/>
<Button
android:text="自定义对话框"
android:onClick="onclick6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
dilog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#303f9e"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:textSize="25sp"
android:text="自定义的对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:text="对话框"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/submit"
android:layout_weight="1"
android:layout_width="0dp"
android:text="确定"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/exit"
android:text="取消"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>