对话框
普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("敌军还有5秒到达战场");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了ok", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了no", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
单选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了ok", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了no", Toast.LENGTH_SHORT).show();
}
});
final String[] aa=new String[]{"河北","河南","天津"};
builder.setSingleChoiceItems(aa, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中了"+aa[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
多选对话框
final String[] s=new String[]{"篮球","足球","乒乓球"};
final boolean[] b={true,true,true};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框");
builder.setIcon(R.mipmap.ic_launcher);
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();
}else {
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();
}
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
自定义内容对话框
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
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();
}
});
View inflate = LayoutInflater.from(this).inflate(R.layout.aaa, null);
builder.setView(inflate);
final AlertDialog dialog = builder.create();
ImageView imageView = inflate.findViewById(R.id.img);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
水平进度对话框
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i=0;
@Override
public void run() {
i+=10;
dialog.setProgress(i);
if (dialog.getProgress()>=dialog.getMax()){
timer.cancel();
dialog.dismiss();
}
}
},0,1000);
}
日期选择对话框
Calendar instance = 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+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
}
}, instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
时间选择对话框
Calendar instance = 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();
}
}, instance.get(Calendar.HOUR), instance.get(Calendar.MINUTE), true);
timePickerDialog.show();
自定义布局对话框
自定义布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#11ffffff">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="警告"
android:textColor="#38ADFF"
android:textSize="16sp"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:text="保护视力,少玩手机"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="取消"
android:textColor="#7D7D7D"
android:textSize="16sp"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4"/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="确定"
android:textColor="#38ADFF"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
一个java类mydialog(自己随便起)继承自dialog:
public class mydialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String xgmessage;
private String xgtitle;
private String xgno;
private String xgyes;
public mydialog(@NonNull Context context) {
super(context);
}
public void setXgmessage(String xgmessage) {
this.xgmessage = xgmessage;
}
public void setXgtitle(String xgtitle) {
this.xgtitle = xgtitle;
}
public void setXgno(String xgno) {
this.xgno = xgno;
}
public void setXgyes(String xgyes) {
this.xgyes = xgyes;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
if (xgmessage!=null){
message.setText(xgmessage);
}
if (xgno!=null){
no.setText(xgno);
}
if (xgtitle!=null){
title.setText(xgtitle);
}
if (xgyes!=null){
yes.setText(xgyes);
}
}
}
按钮调用次java类:
mydialog mydialog = new mydialog(this);
mydialog.setXgmessage("敌军还有5秒到达战场");
mydialog.setXgtitle("王者荣耀");
mydialog.setXgno("开始撤退");
mydialog.setXgyes("发起进攻");
mydialog.show();
让按钮生效:
public interface yesonclick{
void onclick();
}
private yesonclick yesonclick;
public void setYesonclick(mydialog.yesonclick yesonclick) {
this.yesonclick = yesonclick;
}
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesonclick.onclick();
dismiss();
}
});
mydialog.setYesonclick(new mydialog.yesonclick() {
@Override
public void onclick() {
Toast.makeText(MainActivity.this, "阿巴阿巴", Toast.LENGTH_SHORT).show();
}
});