Dialog对话框
一:常用对话框
1.普通对话框
//普通对话框
public void mormal_dialog(View view) {
//构建者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("提示:");//设置标题
builder.setMessage("您确定要删除吗?");//设置内容
//设计确定按钮
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();
}
});
// builder.show();
//使用建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
2.单选对话框
//单选对话框
public void single_dialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("请选择您的出生地?");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您取消了确定", Toast.LENGTH_SHORT).show();
}
});
final String[] items = {"北京","上海","江苏"};
//列表,默认选中下表
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选中:"+items[which], Toast.LENGTH_SHORT).show();
}
});
//创建对话框
AlertDialog alertDialog = builder.create();
//显示对话框
alertDialog.show();
}
3.多选对话框
//多选对话框
public void mulite(View view) {
final String[] items = {"红色","黄色","绿色","蓝色"};
final boolean[] flags = {true,true,true,false};
//构建者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.mipmap.meidui);
builder.setTitle("请选择你喜欢的颜色");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i <flags.length ; i++) {
if (flags[i]){
Toast.makeText(MainActivity.this, "是"+items[i], 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();
}
});
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which] = isChecked;
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
4.自定义对话框
1.布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美国队长"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#ff00"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/meidui"
android:layout_below="@+id/text" />
</RelativeLayout>
2.代码
//自定义对话框
public void customer_dialog(View view) {
View inflate = LayoutInflater.from(this).inflate(R.layout.item, null);
//构建者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置属性
builder.setTitle("复仇者联盟");//设置标题
//设计确定按钮
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();
}
});
builder.setView(inflate);
//使用建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
5.水平进度条对话框
//水平进度条对话框
public void horizontal_dialog(View view) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setMessage("正在下载");
dialog.show();
//模拟定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress==100){
dialog.dismiss();
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);
}
6.圆形进度条对话框
//圆形进度条对话框
public void spinner_dialog(View view) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgress(ProgressDialog.STYLE_SPINNER);
dialog.setMax(100);
dialog.setMessage("正在下载");
dialog.show();
//定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress==100){
dialog.dismiss();
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);//0秒后执行,每隔1000毫秒执行一次
}
7.日期选择对话框
//日期选择对话框
public void date_dialog(View view) {
/*
context 上下文
listener 监听器
year 默认的年
month 默认的月
dayOfMonth 默认的日
*/
//日历对象
Calendar calendar = Calendar.getInstance();
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();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
8.时间选择对话框
//时间选择对话框
public void time(View view) {
/*
context 上下文
listener 监听器
hourOfDay 默认的时
minute 默认的分钟
is24HourView 是否采用24时禁止
*/
Calendar calendar = Calendar.getInstance();
//实例化对象
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).show();
}
二:自定义对话框
1.思路
1.自定义类继承Dialog
2.重写Dialog的方法实例化自定义的对象
3.在Activity中实例化自定义类的对象
2.效果
3.代码
1.布局
<RelativeLayout 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"
>
<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>
2.MyDialog继承Dialog类
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
private Button yes;//确定按钮
private Button no;//取消按钮
private TextView titleTV;//消息标题文本
private TextView message;//消息提示文本
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
//确定文本和取消文本的显示的内容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
public MyDialog(Context context, int themeResId) {
super(context, themeResId);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width = display.getWidth()*4/5;
getWindow().setAttributes(layoutParams);
}
public MyDialog(Context context) {
super(context);
//去掉默认的标题栏
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
}
//设置取消按钮的显示内容和监听
public void setNoOnclickListener(String str,onNoOnclickListener onNoOnclickListener) {
if (str!=null){
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
//设置确定按钮的显示内容和监听
public void setYesOnclickListener(String str,onYesOnclickListener yesOnclickListener){
if (str!=null){
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work__dialog);
//空白处不能取消动画
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();
//初始化界面控件的事件
initEvent();
}
//初始化界面控件
private void initView() {
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
titleTV = findViewById(R.id.title);
message = findViewById(R.id.message);
}
//初始化界面控件的显示数据
private void initData() {
if (titleStr!=null){
titleTV.setText(titleStr);
}
if (messageStr!=null){
message.setText(messageStr);
}
//如果设置按钮文字
if (yesStr!=null){
yes.setText(yesStr);
}
if (noStr!=null){
no.setText(noStr);
}
}
//初始化界面的确定和取消监听
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener!=null){
yesOnclickListener.onYesOnclick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener!=null){
noOnclickListener.onNoClick();
}
}
});
}
//从外界Activity为Dialog设置标题
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
public interface onNoOnclickListener{
public void onNoClick();
}
public interface onYesOnclickListener{
public void onYesOnclick();
}
}
public class Work_Dialog extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work__dialog);
MyDialog myDialog = new MyDialog(this);
myDialog.setTitle("Hello");
myDialog.setMessage("失落的一角");
myDialog.setNoOnclickListener("no", new MyDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
Toast.makeText(Work_Dialog.this, "no", Toast.LENGTH_SHORT).show();
}
});
myDialog.setYesOnclickListener("yes", new MyDialog.onYesOnclickListener() {
@Override
public void onYesOnclick() {
Toast.makeText(Work_Dialog.this, "yes", Toast.LENGTH_SHORT).show();
}
});
myDialog.show();
}
}