Android 根本Dialog和自定义Dialog
Dialog类是对话框的基类,但你应该防止间接实例化Dialog ,能够应用子类
1.AlertDialog 此对话框能够显示题目,最多三个按钮,可抉择列表或自定义布局View.
2.DatePickerDialog或TimePickerDialog 此对话框带有容许用户抉择日期或工夫的预约义布局
3.留神:Android 蕴含另一种名为 ProgressDialog 的对话框类,该类可显示带有进度条的对话框。此微件已弃用,因为它会在显示进度的状况下阻止用户与利用互动。如果须要批示加载进度或不确定的进度,您应遵循进度和 Activity的设计准则,并在布局中应用ProgressBar,而非 ProgressDialog。
一:创立AlertDialog
1.创立结构器AlertDialog.Builder的对象;
2.通过结构器对象调用setTitle、setMessage、setIcon等办法结构对话框的题目、信息和图标等内容;
3.依据须要调用setPositive/Negative/NeutralButton()办法设置侧面按钮、负面按钮和中立按钮;
4.调用结构器对象的create办法创立AlertDialog对象;
5.AlertDialog对象调用show办法,让对话框在界面上显示。
注:AlertDialog.Builder本人也有一个show办法,能够显示对话框,所以下面的第4、第5步能够简化为一步
二:创立两个显示2个按钮的Dialog
<?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"> <Button android:id="@+id/btn_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="最一般的 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="三个按钮的 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_multi_select" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_single_select" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_waiting" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="期待 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_loading" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="输出 dialog" android:textAllCaps="false" /> <Button android:id="@+id/btn_my_style" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义 dialog" android:textAllCaps="false" /></LinearLayout>
FristActivity中创立显示2个按钮dialog
private AlertDialog.Builder builder;/** * 显示两个按钮 */private void showTwo() { builder = new AlertDialog.Builder(this) .setIcon(R.mipmap.ic_launcher) .setTitle("两个按钮的dialog") .setMessage("我是两个按钮的dialog内容") .setCancelable(true)//点击对话框以外的区域是否让对话框隐没 .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //ToDo: 你想做的事件 Toast.makeText(FirstActivity.this, "确定按钮", Toast.LENGTH_LONG).show(); } }).setNegativeButton("勾销", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //ToDo: 你想做的事件 Toast.makeText(FirstActivity.this, "敞开按钮", Toast.LENGTH_LONG).show(); // dialogInterface.dismiss(); } }); builder.create().show();}
成果:
三:三个按钮的Dialog
private void showThree() { /** * 三个按钮的 dialog */ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher) .setTitle("三个按钮dialog题目") .setMessage("三个按钮dialog内容") .setPositiveButton("确定(踊跃)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //ToDo: 你想做的事件 Toast.makeText(FirstActivity.this, "确定按钮", Toast.LENGTH_LONG).show(); } }) .setNeutralButton("你猜(中立)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(FirstActivity.this, "你猜按钮", Toast.LENGTH_LONG).show(); } }) .setNegativeButton("勾销(消极)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //ToDo: 你想做的事件 Toast.makeText(FirstActivity.this, "敞开按钮", Toast.LENGTH_LONG).show(); dialogInterface.dismiss(); } }); AlertDialog dialog=builder.create(); //对话框隐没的监听事件 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { Log.e("TAG","对话框隐没了"); } }); //对话框显示的监听事件 dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Log.e("TAG","对话框显示了"); } }); //显示对话框 dialog.show();}
咱们对Dialog 监听dialog 是否显示
dialog.setOnShowListener 和 dialog.setOnCancelListener
// E/TAG: 对话框显示了
四:列表抉择Item Dialog
/** * 列表单选抉择 dialog */int checkedItem = 0;private void showList() { //默认选中的item final String[] items = {"西湖区", "江干区", "上城区", "下城区", "拱墅区", "滨江区", "萧山区"}; builder = new AlertDialog.Builder(this) .setIcon(R.mipmap.ic_launcher) .setTitle("列表抉择dialog") .setCancelable(false) .setSingleChoiceItems(items,checkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { checkedItem=i; Toast.makeText(FirstActivity.this, "你点击的内容为: " + items[i], Toast.LENGTH_LONG).show(); } }); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("勾销", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show();}
成果:
五:多选列表Dialog
private void showMultiSelect() { final List<Integer> choice = new ArrayList<>(); final String[] items = {"多选1", "多选2", "多选3", "多选4", "多选5", "多选6"}; //默认都未选中 boolean[] isSelect = {false, false, false, false, false, false}; builder = new AlertDialog.Builder(this) .setIcon(R.mipmap.ic_launcher) .setTitle("多选dialog") .setMultiChoiceItems(items, isSelect, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { if (b) { choice.add(i); } else { choice.remove(choice.indexOf(i)); } } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { StringBuilder str = new StringBuilder(); for (int j = 0; j < choice.size(); j++) { str.append(items[choice.get(j)]); } Toast.makeText(FirstActivity.this, "你抉择了" + str,Toast.LENGTH_LONG).show(); } }); builder.create().show();}
成果:
六:显示进度条的Dialog
/**显示进度条的Dialog*/ private void showLoading() { final int MAX_VALUE = 100; progressDialog = new ProgressDialog(this); progressDialog.setProgress(0); progressDialog.setTitle("带有加载进度dialog"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(MAX_VALUE); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { int progress = 0; while (progress < MAX_VALUE) { try { Thread.sleep(100); progress++; progressDialog.setProgress(progress); } catch (InterruptedException e) { e.printStackTrace(); } } //加载结束主动敞开dialog progressDialog.cancel(); } }).start(); } //这种会妨碍用户的交互,当初应用ProgressBar
七:自定义View的Dialog
private void showMyStyle() { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View dialogView = View.inflate(this, R.layout.dialog_login, null); //设置对话框布局 dialog.setView(dialogView); EditText etName = (EditText) dialogView.findViewById(R.id.et_name); EditText etPwd = (EditText) dialogView.findViewById(R.id.et_pwd); Button btnLogin = (Button) dialogView.findViewById(R.id.btn_login); Button btnCancel = (Button) dialogView.findViewById(R.id.btn_cancel); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String name = etName.getText().toString(); final String pwd = etPwd.getText().toString(); if (TextUtils.isEmpty(name) && TextUtils.isEmpty(pwd)) { Toast.makeText(FirstActivity.this, "用户名和明码均不能为空", Toast.LENGTH_SHORT).show(); return; } Log.e("TAG", "用户名:" + name); Log.e("TAG", "明码:" + pwd); dialog.dismiss(); } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show();}
对应的布局文件
<?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/textView" android:layout_width="match_parent" android:layout_height="50dp" android:background="#169ee5" android:gravity="center" android:text="请先登录" android:textColor="@android:color/white" android:textSize="20sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输出你的账户名称" android:textSize="18sp" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输出你的明码" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:orientation="horizontal" android:paddingLeft="5dp" android:paddingRight="5dp"> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="1" android:background="#169ee5" android:text="勾销" android:textColor="@android:color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#169ee5" android:text="登录" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout></LinearLayout>
成果:
点击确认后:
E/TAG: 用户名:admin E/TAG: 明码:admin
自定义对话框须要留神的问题:
零碎dialog的宽度默认是固定的,即便你自定义布局怎么批改宽度也不起作用,高度可依据布局主动调节。如果想批改弹出窗体大小,能够应用上面这段代码来实现扭转对话框的宽高。这段代码必dialog.show()办法之后调用才无效。
AlertDialog.Builder builder = new AlertDialog.Builder(this);final AlertDialog dialog = builder.setCancelable(false).create();View dialogView = View.inflate(this, R.layout.dialog_login, null);//设置对话框布局dialog.setView(dialogView);dialog.show();dialog.getWindow().setLayout(ScreenUtils.getScreenWidth(this)/2,LinearLayout.LayoutParams.WRAP_CONTENT);//通过此形式来设置dialog 的宽高
屏幕工具类
public class ScreenUtils { /** * 获取屏幕高度(px) */ public static int getScreenHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } /** * 获取屏幕宽度(px) */ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; }}
扭转Android Dialog 弹出后的Activity背景亮度:在代码中批改lp.alpha大小,值的大小可依据本人要求设置
// 设置屏幕背景变暗private void setScreenBgDarken() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.5f; lp.dimAmount = 0.5f; getWindow().setAttributes(lp);}// 设置屏幕背景变亮private void setScreenBgLight() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1.0f; lp.dimAmount = 1.0f; getWindow().setAttributes(lp);}
管制弹窗弹出的地位:个别都是在屏幕正中间弹出默认,但能够管制从别的中央弹出,比方从底部弹出
private void popFromBottom(Dialog dialog) { Window win = dialog.getWindow(); win.setGravity(Gravity.BOTTOM); // 这里管制弹出的地位 win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // dialog.getWindow().setBackgroundDrawable(null);//这是设置window背景为空 win.setAttributes(lp);}
八:TimePickerDialog应用
同日期选择器一样,工夫选择器也有一般控件和弹框控件两种形式,而且理论开发中也是很少用一般控件(TimePicker),而是罕用封装好的TimePickerDialog,该对话框的用法相似于DatePickerDialog,不同之处有两个:
(1)构造函数传的是以后的小时与分钟,最初一个参数示意是否采纳二十四小时制,个别传true,示意小时数范畴0~23
(2)工夫抉择监听器是OnTimeSetListenser,对应须要实现的办法是onTimeSet,在该办法中能够取得用户选好的小时和分钟
/**工夫抉择Dialog*/ private void showMyTime() { //获取日历的一个实例,外面蕴含了以后的时分秒 Calendar calendar=Calendar.getInstance(); //构建一个工夫对话框,该对话框曾经集成了工夫选择器 //TimePickerDialog的第二个结构参数指定了事件监听器 TimePickerDialog dialog=new TimePickerDialog(this,this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);//true示意应用二十四小时制,第一个是Context,第二个参数监听TimePickerDialog.OnTimeSetListener //把工夫对话框显示在界面上 dialog.show(); }
实现接口TimePickerDialog.OnTimeSetListener
public class FirstActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) { //获取工夫对话框设定的小时和分钟数 String desc=String.format("您抉择的工夫是%d时%d分",hourOfDay,minute); Toast.makeText(FirstActivity.this, desc, Toast.LENGTH_LONG).show();}}
成果:
九:DatePickerDialog 应用
private void showMyDate() { //获取日历的一个实例,外面蕴含了以后的年月日 Calendar calendar=Calendar.getInstance(); //构建一个日期对话框,该对话框曾经集成了日期选择器 //DatePickerDialog的第二个结构参数指定了日期监听器 DatePickerDialog dialog=new DatePickerDialog(this,this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); //把日期对话框显示在界面上 dialog.show();}
实现接口DatePickerDialog.OnDateSetListener
public class FirstActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { //获取日期对话框设定的年月份 String desc=String.format("您抉择的日期是%d年%d月%d日",year,month+1,dayOfMonth); Toast.makeText(FirstActivity.this, desc, Toast.LENGTH_LONG).show();}}
成果:
十:齐全自定义Dialog应用
继承Dialog 实现dialog性能
public class LovelyAlertDialog extends Dialog { protected LinearLayout dialogRoot; protected Context mContext; public LovelyAlertDialog(@NonNull Builder builder) { super(builder.mContext); dialogRoot = (LinearLayout) builder.view; mContext = builder.mContext; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(dialogRoot); setCanceledOnTouchOutside(true); } public static class Builder { protected Context mContext; private View view; private TextView contentTitle;//信息题目 private TextView contentMsg;//信息内容 protected TextView btnCancel;//勾销protected TextView btnConfirm;//确认DialogInterface.OnClickListener onCancelClickListener;public Builder(Context context) { this.mContext = context;}public Builder getView(int resView) { view = LayoutInflater.from(mContext).inflate(resView, null); return this;}/** * 设置弹窗题目 */public final Builder setTitle(int viewRes, String title) { contentTitle = view.findViewById(viewRes); if (StringUtils.isTrimEmpty(title)) { contentTitle.setVisibility(View.GONE); } else { contentTitle.setVisibility(View.VISIBLE); contentTitle.setText(title); } return this;}/**设置确认按钮监听*/public final Builder setOnConfirmClickListener(int viewRes, String contentConfirmBtn, View.OnClickListener onConfirmClickListener) { btnConfirm=view.findViewById(viewRes); if (StringUtils.isTrimEmpty(contentConfirmBtn)){ btnConfirm.setText("确认"); }else { btnConfirm.setText(contentConfirmBtn); } if (onConfirmClickListener!=null){ btnConfirm.setOnClickListener(onConfirmClickListener); } return this;}/**设置勾销按钮监听*/public final Builder setOnCancelClickListener(int viewRes, String contentCancelBtn, OnClickListener onCancelClickListener) { btnCancel=view.findViewById(viewRes); if (StringUtils.isTrimEmpty(contentCancelBtn)){ btnCancel.setText("勾销"); }else { btnCancel.setText(contentCancelBtn); } if (onCancelClickListener!=null){ this.onCancelClickListener=onCancelClickListener; btnCancel.setOnClickListener(v -> {onCancelClickListener.onClick(new LovelyAlertDialog(this),-2);}); } return this;}public LovelyAlertDialog create() { LovelyAlertDialog dialog = new LovelyAlertDialog(this); return dialog;}
END:学道之难,难于上青天!