常用对话框
普通对话框
public void btn(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
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 = builder.create();
alertDialog.show();
}
单选对话框
public void btn1(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
final String[] s=new String[]{"篮球","足球","学习"};
builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
});
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 = builder.create();
alertDialog.show();
}
多选对话框
public void btn2(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
final String[] s=new String[]{"篮球","足球","学习"};
final boolean[] b=new boolean[]{false,false,false};
builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < b.length; i++) {
if (b[i]){
Toast.makeText(MainActivity.this, s[i], 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 = builder.create();
alertDialog.show();
}
进度条对话框
public void btn4(View view) {
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setTitle("标题");
progressDialog.setMax(100);
progressDialog.setProgress(10);
progressDialog.setSecondaryProgress(20);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
int index=0;
@Override
public void run() {
index+=10;
progressDialog.show();
progressDialog.setProgress(index);
if (index>100){
timer.cancel();
progressDialog.dismiss();
}
}
},0,1000);
progressDialog.show();
}
自定义对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:text="机器人"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content"/>
</LinearLayout>
public void btn3(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
View inflate = LayoutInflater.from(this).inflate(R.layout.item, null);
builder.setView(inflate);
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 AlertDialog alertDialog = builder.create();
ImageView imageView = inflate.findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
完全自定义对话框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="300dp"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="警告"
android:textSize="20sp" />
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="少玩游戏,保护眼睛"
android:layout_marginTop="10dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
android:background="@color/white"
android:textSize="30sp"/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"
android:background="@color/white"
android:textSize="30sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
public class MyTask extends Dialog {
private TextView ti;
private TextView msg;
private Button no;
private Button yes;
public MyTask(@NonNull Context context) {
super(context);
}
private String strti;
public void setStrti(String strti) {
this.strti = strti;
}
private String strMsg;
public void setStrMsg(String strMsg) {
this.strMsg = strMsg;
}
public interface YesOnClick{
void click();
}
private YesOnClick yesOnClick;
public void setYesOnClick(YesOnClick yesOnClick) {
this.yesOnClick = yesOnClick;
}
public interface NoOnClick{
void click();
}
private NoOnClick noOnClick;
public void setNoOnClick(NoOnClick noOnClick) {
this.noOnClick = noOnClick;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item2);
initView();
ti.setText(strti);
msg.setText(strMsg);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
yesOnClick.click();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
noOnClick.click();
}
});
}
private void initView() {
ti = (TextView) findViewById(R.id.ti);
msg = (TextView) findViewById(R.id.msg);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
}
}
日期对话框
public void btn5(View view) {
Calendar calendar=Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year + "年" + month + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
时间对话框
public void btn6(View view) {
Calendar calendar=Calendar.getInstance();
TimePickerDialog timePickerDialog = 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);
timePickerDialog.show();
}