普通对话框;
单选对话框;
多选对话框;
水平进度对话框;
圆形进度对话框;
时间对话框;
日期对话框;
自定义对话框;
setIcon——设置图标;
setTitle——设置标题;
setMessage——设置内容;
setPositiveButton——设置点击确认的监听;
setNegativeButton——设置点击取消的监听;
主类
// An highlighted block
public class MainActivity extends AppCompatActivity {
//八个按钮对应八个对话框
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到相对应的控件
b1=findViewById(R.id.main_button1);
b2=findViewById(R.id.main_button2);
b3=findViewById(R.id.main_button3);
b4=findViewById(R.id.main_button4);
b5=findViewById(R.id.main_button5);
b6=findViewById(R.id.main_button6);
b7=findViewById(R.id.main_button7);
b8=findViewById(R.id.main_button8);
//new出创建的点击类
MyOnclick myOnclick=new MyOnclick();
//给每个控件设置点击事件
b1.setOnClickListener(myOnclick);
b2.setOnClickListener(myOnclick);
b3.setOnClickListener(myOnclick);
b4.setOnClickListener(myOnclick);
b5.setOnClickListener(myOnclick);
b6.setOnClickListener(myOnclick);
b7.setOnClickListener(myOnclick);
b8.setOnClickListener(myOnclick);
}
自己写的点击类
// 自己写一个类继承View.OnClickListener,重写onClick方法
public class MyOnclick implements View.OnClickListener{
@Override
public void onClick(View v) {
//switch判断每一个控件的id并设置相对应的方法
switch (v.getId()){
case R.id.main_button1:
normal_Dialog();
break;
case R.id.main_button2:
Single_Dialog();
break;
case R.id.main_button3:
Check_Dialog();
break;
case R.id.main_button4:
date_dialog();
break;
case R.id.main_button5:
time_dialog();
break;
case R.id.main_button6:
HorProgrssbar_Dialog();
break;
case R.id.main_button7:
SpinnProgress_Dialog();
break;
case R.id.main_button8:
Customer_Dialog();
break;
}
}
}
普通对话框方法
public void normal_Dialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.jj);//设置图标
builder.setTitle("这是普通框");//设置title
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 alertDialog=builder.create();
//点击对话框之外的地方不消失
alertDialog.setCanceledOnTouchOutside(false);
//设置显示
alertDialog.show();
}
单选对话框方法
public void Single_Dialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.jj);
builder.setTitle("这是单选框");
//切记单选,复选对话框不能设置内容
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();
}
});
//创建单选的数据
final String[] str=new String[]{"上海","北京","重庆","广州","天津"};
//设置单选对话框的监听
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.setCanceledOnTouchOutside(false);
alertDialog.show();
}
多选对话框方法
public void Check_Dialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.jj);
builder.setTitle("这是复选对话框");
//切记单选,复选不能设置内容
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();
}
});
//设置复选数据
final String[] str=new String[]{"上海","北京","重庆","广州","天津"};
//设置复选的数据是否选中,其中复选数据有几个boolean就有几个
final boolean[] booleans=new boolean[]{false,false,false,false,false};
//设置复选监听
builder.setMultiChoiceItems(str, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this,"你选中了"+str[which],Toast.LENGTH_SHORT).show();
booleans[which]=isChecked;
}
});
AlertDialog alertDialog=builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
水平进度对话框方法
public void HorProgrssbar_Dialog(){
final ProgressDialog progressDialog=new ProgressDialog(this);
//设置进度条类型,这是水平
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIcon(R.drawable.jj);
progressDialog.setTitle("这是进度对话框");
progressDialog.setMax(100);//设置进度的最大值
progressDialog.setMessage("正在加载");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
//设置每隔1秒加载10
CountDownTimer countDownTimer=new CountDownTimer(11000,1000) {
int progress=0;
@Override
public void onTick(long millisUntilFinished) {
progressDialog.setProgress(progress+=10);
}
@Override
public void onFinish() {
progressDialog.setMessage("已完成");
//完成时对话框消失
progressDialog.dismiss();
}
}.start();
}
圆形进度对话框方法
public void SpinnProgress_Dialog(){
final ProgressDialog progressDialog=new ProgressDialog(this);
//设置进度条类型,圆形
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMax(100);
progressDialog.setMessage("正在加载");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
Timer timer=new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if(progress==100){
progressDialog.dismiss();
progressDialog.cancel();
}
progressDialog.setProgress(progress+=10);
}
},0,1000);
}
日期对话框方法
public void date_dialog(){
//创建Canlendar对象
Calendar calendar=Calendar.getInstance();
//添加监听
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)).show();
}
时间对话框方法
public void time_dialog(){
//创建Canlendar对象
Calendar calendar=Calendar.getInstance();
//添加监听
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),true).show();
}
自定义对话框方法
public void Customer_Dialog(){
//填充自己编写的布局文件
View view= LinearLayout.inflate(MainActivity.this,R.layout.customer,null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.jj);
builder.setTitle("这是自定义对话框");
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();
}
});
//添加布局
builder.setView(view);
AlertDialog alertDialog=builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
Xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通"
/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button2"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button3"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button4"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button5"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button6"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平进度"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button7"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆形进度"
/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/main_button8"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
//自定义布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义"
android:textSize="30sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jj"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>