DialogActivity.java
package cn.edu.zufe.app001;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
public class DialogActivity extends AppCompatActivity {
final int COMMON_DAILOG = 1;
final int LIST_DIALOG = 2;
final int LIST_DIALOG_SINGLE = 3;
final int LIST_DIALOG_MULTIPLE = 4;
boolean[] mulFlags = new boolean[]{true,false,true};
String[] items = null;
final int PROGRESS_DIALOG = 5;
final int INCREASE = 5;
ProgressDialog pd;
Handler myHandler;
final int DATE_DIALOG = 6;
final int TIME_DIALOG = 7;
Calendar c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
items = getResources().getStringArray(R.array.msa);
this.myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case INCREASE:
pd.incrementProgressBy(1);
if(pd.getProgress() >= 100){
pd.dismiss();
}
break;
}
super.handleMessage(msg);
}
};
Button btn01 = (Button)findViewById(R.id.button1);
btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(COMMON_DAILOG);
}
});
Button btn02 = (Button)findViewById(R.id.button2);
btn02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(LIST_DIALOG);
}
});
Button btn03 = (Button)findViewById(R.id.button3);
btn03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(LIST_DIALOG_SINGLE);
}
});
Button btn04 = (Button)findViewById(R.id.button4);
btn04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(LIST_DIALOG_MULTIPLE);
}
});
Button btn05 = (Button)findViewById(R.id.button5);
btn05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG);
}
});
Button btn06 = (Button)findViewById(R.id.button6);
btn06 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(PROGRESS_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder b = new AlertDialog.Builder(this);
switch(id){
case COMMON_DAILOG:
b.setIcon(R.drawable.ic_header);
b.setTitle(R.string.title1);
b.setMessage(R.string.dialg_msg1);
b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et01 = (EditText)findViewById(R.id.et_name);
et01.setText(R.string.dialg_msg1);
}
});
dialog = b.create();
break;
case LIST_DIALOG:
b.setIcon(R.drawable.ic_header);
b.setTitle(R.string.title2);
b.setItems(R.array.msa, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et01 = (EditText)findViewById(R.id.et_name);
et01.setText("您选择了" + getResources().getStringArray(R.array.msa)[which]);
}
});
dialog = b.create();
break;
case LIST_DIALOG_SINGLE:
b.setIcon(R.drawable.ic_header);
b.setTitle(R.string.title3);
b.setSingleChoiceItems(R.array.msa, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et01 = (EditText)findViewById(R.id.et_name);
et01.setText("您选择了" + getResources().getStringArray(R.array.msa)[which]);
}
});
b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog = b.create();
break;
case LIST_DIALOG_MULTIPLE:
b.setIcon(R.drawable.ic_header);
b.setTitle(R.string.title4);
b.setMultiChoiceItems(R.array.msa, mulFlags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mulFlags[which] = isChecked;
String result = "您选择了:";
for(int i=0 ;i<mulFlags.length; i++){
if(mulFlags[i]){
result += items[i] + ",";
}
}
EditText et01 = (EditText)findViewById(R.id.et_name);
et01.setText(result.substring(0,result.length()-1));
}
});
b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog = b.create();
break;
case DATE_DIALOG:
c = Calendar.getInstance();
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
EditText et01 = (EditText)findViewById(R.id.et_name);
et01.setText("您选择了" + year + "年" + (month +1) + "月" + dayOfMonth + "日");
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
break;
case PROGRESS_DIALOG:
pd = new ProgressDialog(this);
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle(R.string.titleok);
pd.setMessage("请稍等...");
pd.setCancelable(false);
dialog = pd;
break;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
if(id == PROGRESS_DIALOG){
pd.incrementProgressBy(-pd.getProgress());
new Thread(){
public void run(){
while(true){
myHandler.sendEmptyMessage(INCREASE);
if(pd.getProgress() >= 100){
break;
}
try{
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
super.onPrepareDialog(id, dialog);
}
}
activity_dialog.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="cn.edu.zufe.app001.DialogActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/et_name">
</EditText>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="显示普通对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="显示列表对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="显示单选对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:text="显示多选对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:text="显示日期选择对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button6"
android:text="显示进度条对话框"
android:layout_gravity="center"
android:textSize="20sp">
</Button>
</LinearLayout>