自定义Dialo:
Dialog按钮分为三种:NegativeButton(消极),NeutralButton(中性),PositiveButton(积极)
对话框标题:setTitle(“内容”)
设置Dialog的提示图标:setIcon(R.mipmap.ic_launcher)
设置内容:setMessage(“Dialog的提示内容”)
点击其他位置是否退出:setCancelable(false)
设置进度条Dialog的样式:setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
设置Dialog的进度最大值:setMax(100)
设置Dialog初始的进度值:setProgress(0);
Dialog有10种:普通Dialog,单选Dialog,多选Dialog,可输入Dialog,年份Dialog(年月日),时间Dialog(时分秒),多个按钮Dialog,完全自定义Dialog,进度条Dialog,列表Dialog
package com.example.day01;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Button button1,button2,button3,button4,button5,button6,button7,button8,button9;
private TextView textView;
private String[] name={"上","中","下"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
button5=findViewById(R.id.button5);
button4=findViewById(R.id.button4);
button8=findViewById(R.id.button8);
button9=findViewById(R.id.button9);
}
public void ALERTDIALOG(View view) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
**//对话框标题**
.setTitle("简单对话框")
**//设置图标**
.setIcon(R.mipmap.ic_launcher)
**//设置内容**
.setMessage("测试对话框内容\n第二行内容")
**//点击其他位置是否退出**
.setCancelable(false);
alertDialog.setNegativeButton("消极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了取消");
}
});
alertDialog.setNeutralButton("中性", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了功能");
}
});
alertDialog.setPositiveButton("积极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了确定");
}
});
AlertDialog alertDialog1 = alertDialog.create();
alertDialog.show();
}
public void MuLiTeDialog(View view) {
final boolean[] booleans = new boolean[]{false, false, false};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("你要去哪")
.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(name, booleans, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
booleans[which]=isChecked;
}
})
.setPositiveButton("积极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了确定");
}
})
.setNegativeButton("消极", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了取消");
}
});
builder.create().show();
}
public void SingleDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("你想去哪")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(name, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("你选了"+name[which]);
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
public void ListDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("自定义列表对话框")
.setIcon(R.mipmap.ic_launcher)
.setAdapter(new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,name), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("点击了列表");
}
});
builder.create().show();
}
int count=0;
public void ProgessDialog(View view) {
count=0;
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setMessage("加载中...");
progressDialog.show();
**// 注:需要设置进度条来更新进度值**
new CountDownTimer(11000,1000){
@Override
public void onTick(long millisUntilFinished) {
progressDialog.setProgress(count+=10);
}
@Override
public void onFinish() {
progressDialog.setMessage("已完成");
progressDialog.dismiss();
}
}.start();
}
public void TimeDialog(View view) {
final Calendar instance = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textView.setText("当前时间"+hourOfDay+"时"+minute);
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
//注:,instance.get(Calendar.HOUR)为当前的小时,instance.get(Calendar.MINUTE)为当前的分钟,true是否为24小时制
}
public void DateDialog(View view) {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
textView.setText("当前日期:"+(month+1)+"月"+dayOfMonth+"日");
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)).show();
}
}