DiaLog对话框
一。常用的对话框
1.普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("标题")
.setMessage("请选择");
builder.setNegativeButton("选", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了", 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();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
2.单选对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("标题");
final String[] item = new String[] {"1号","2号","3号"};
builder.setSingleChoiceItems(item, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
a = which;
}
});
builder.setNegativeButton("选", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了"+item[a], 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();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
3.多选对话框
直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC
语法后生成一个完美的目录。
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("标题");
final String[] item = new String[] {"1号","2号"};
final boolean[] b = new boolean[]{true,false};
builder.setMultiChoiceItems(item, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
b[which] = isChecked;
}
});
builder.setNegativeButton("选", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了"+item[a], 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.create().show();
4.自定义对话框
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.image_layout, null);
ImageView imageView = inflate.findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("标题");
builder.setView(inflate);
alertDialog = builder.create();
alertDialog.show();
(1).自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="图片"
android:textSize="50sp"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
</LinearLayout>
(2).Java代码:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.image_layout, null);
ImageView imageView = inflate.findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("标题");
builder.setView(inflate);
alertDialog = builder.create();
alertDialog.show();
5.水平进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setMessage("正在下载");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(progress == 100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(progress+=20);
}
},0,1000);
6.圆形进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(RatingBar.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
progressDialog.setMax(100);
progressDialog.setMessage("正在下载");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(progress == 100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(progress+=20);
}
},0,1000);
}
});
7.日期选择对话框
final Calendar calendar = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
8.时间选择对话框
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 11, 11, true).show();
二。自定义对话框
1.思路:
1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义类的对象
2.代码:
(1).xml布局:自定义对话框的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/div_tvbt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="警告"
android:textSize="50sp"/>
<TextView
android:id="@+id/div_tvts"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="少玩手机,保护视力"
android:textSize="20sp"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/div_btqx"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
<Button
android:id="@+id/div_btqd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"/>
</LinearLayout>
</LinearLayout>
(2)自定义类继承dialog
public class DivDialog extends Dialog {
private TextView divTvbt;
private TextView divTvts;
private Button divBtqx;
private Button divBtqd;
private String btStr;
private String tsStr;
private String yesStr;
private String noStr;
private onYesListener onYesListener;
private onNoListener onNoListener;
public DivDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.div_layout);
divTvbt = (TextView) findViewById(R.id.div_tvbt);
divTvts = (TextView) findViewById(R.id.div_tvts);
divBtqx = (Button) findViewById(R.id.div_btqx);
divBtqd = (Button) findViewById(R.id.div_btqd);
if(btStr != null){
divTvbt.setText(btStr);
}
if(tsStr != null){
divTvts.setText(tsStr);
}
if(yesStr != null){
divBtqd.setText(yesStr);
}
if(noStr != null){
divBtqx.setText(noStr);
}
divBtqd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onYesListener != null){
onYesListener.onYesClick();
dismiss();
}
}
});
divBtqx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onNoListener != null){
onNoListener.onNoClick();
dismiss();
}
}
});
}
public void setBtStr(String btStr) {
this.btStr = btStr;
}
public void setTsStr(String tsStr) {
this.tsStr = tsStr;
}
public void setYesStr(String yesStr) {
this.yesStr = yesStr;
}
public void setNoStr(String noStr) {
this.noStr = noStr;
}
public void setOnYesListener(DivDialog.onYesListener onYesListener) {
this.onYesListener = onYesListener;
}
public void setOnNoListener(DivDialog.onNoListener onNoListener) {
this.onNoListener = onNoListener;
}
public interface onYesListener{
public void onYesClick();
};
public interface onNoListener{
public void onNoClick();
};
(3).实现代码
DivDialog divDialog = new DivDialog(MainActivity.this);
divDialog.setBtStr("标题");
divDialog.setTsStr("提示");
divDialog.setYesStr("Yes");
divDialog.setNoStr("No");
divDialog.setOnYesListener(new DivDialog.onYesListener() {
@Override
public void onYesClick() {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
});
divDialog.setOnNoListener(new DivDialog.onNoListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
}
});
divDialog.show();