Android Dialog使用、自定义带动画的Dialog使用
来源:互联网 发布:知美术馆电话 编辑:程序博客网 时间:2024/05/01 07:29
效果图
普通Dialog
private void showNormalDialog() { final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher) .setTitle("普通Dialog") .setMessage("这是一个普通的Dialog") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); //添加中间按钮 就有三个按钮 //normalDialog.setNeutralButton }
列表Dialog
private void showListDialog() { final String[] items = {"按钮1", "按钮2", "按钮3", "按钮4"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("列表Dialog") .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); }
单选Dialog
private void showSingleChoiceDialog() { final String[] items = {"按钮1", "按钮2", "按钮3", "按钮4"}; final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("单选Dialog") .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); }
多选Dialog
final ArrayList<Integer> choices = new ArrayList<>(); private void showMultiChoiceDialog() { final String[] items = {"按钮1", "按钮2", "按钮3", "按钮4"}; // 设置默认选中的选项 final boolean initChoiceSets[] = {false, false, false, false}; //choices.clear(); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("多选Dialog") .setMultiChoiceItems(items, initChoiceSets, new DialogInterface .OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { choices.add(which); } else { //有问题:是把whichButton作为index还是Object?实际上会抛下标溢出异常 //MultiChoiceID.remove(whichButton); //需强制转换为Object类型,才会以对象的形式删除;否则会作为下标处理 choices.remove((Integer) which); } } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); }
等待Dialog
private void showWaitingDialog() { final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("等待Dialog"); progressDialog.setMessage("等待中..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(false); progressDialog.show(); new Thread() { @Override public void run() { super.run(); SystemClock.sleep(3000); progressDialog.dismiss(); } }.start(); }
进度条Dialog
private void showProgressDialog() { final int MAX_PROGRESS = 100; final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setCancelable(false); progressDialog.setProgress(0); progressDialog.setTitle("进度条Dialog"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(MAX_PROGRESS); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { int progress = 0; while (progress < MAX_PROGRESS) { try { Thread.sleep(10); progress++; //子线程可以更新progressDialog进度 progressDialog.setProgress(progress); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog.cancel(); } }).start(); }
编辑Dialog
private void showEditDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final EditText view = new EditText(MainActivity.this); builder.setTitle("输入dialog") .setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String content = view.getText().toString(); } }) .show(); }
自定义带动画的Dialog
private void showConsumerDialog() { Dialog dialog = new Dialog(MainActivity.this, R.style.BottomDialog); View view = LayoutInflater.from(this).inflate(R.layout.dialog, null); dialog.setContentView(view); ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.width = getResources().getDisplayMetrics().widthPixels; view.setLayoutParams(layoutParams); dialog.getWindow().setGravity(Gravity.BOTTOM); dialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation); dialog.setCanceledOnTouchOutside(true); dialog.show(); }
自定义带动画的圆角Dialog
private void showConsumerDialogCircle() { final Dialog dialog = new Dialog(MainActivity.this, R.style.BottomDialog); View view = LayoutInflater.from(this).inflate(R.layout.dialog_circle, null); view.findViewById(R.id.takePhoto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); view.findViewById(R.id.selectPhoto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.setContentView(view); ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view .getLayoutParams(); layoutParams.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()); layoutParams.width = getResources().getDisplayMetrics().widthPixels - (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()); view.setLayoutParams(layoutParams); dialog.getWindow().setGravity(Gravity.BOTTOM); dialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation); dialog.setCanceledOnTouchOutside(true); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); dialog.show(); }
- 自定义dialog布局文件 dialog_circle.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@drawable/shape_dialog" android:layout_height="wrap_content"> <TextView android:id="@+id/takePhoto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:padding="16dp" android:textSize="14sp" android:text="拍照"/> <TextView android:layout_width="match_parent" android:id="@+id/selectPhoto" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:padding="16dp" android:textSize="14sp" android:text="从相册选取"/></LinearLayout>
- dialog_circle.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp"/> <solid android:color="@android:color/white"/></shape>
- style
<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style> <style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog"> <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item> <item name="android:windowExitAnimation">@anim/translate_dialog_out</item> </style>
- Enter Exit 动画
translate_dialog_in.xml<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="100%" android:toXDelta="0" android:toYDelta="0"></translate>translate_dialog_out.xml<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="100%"></translate>
github https://github.com/REIGE/DialogDemo
0 0
- Android Dialog使用、自定义带动画的Dialog使用
- android 自定义dialog 带动画的
- Android 自定义带动画的dialog
- Android自定义Dialog带Dialog的显示消失动画(一)
- Android 自定义Dialog使用
- 自定义dialog的使用
- Android中的Dialog的使用以及自定义Dialog
- Android中自定义Dialog的使用
- android 使用DialogFragment 自定义Dialog
- Android Dialog的使用
- Android Dialog 的使用
- android dialog的使用
- Android Dialog大全 Dialog的使用
- DialogFragment的使用(自定义Dialog )
- Dialog使用自定义的样式
- android 自定义带关闭按钮的dialog
- Android自定义底部带有动画的Dialog
- Android 自定义dialog 和 相应的动画
- 2014蓝桥 李白打酒
- CSDN博客积分规则
- bzoj 4586: [Usaco2016 Open]Landscaping 堆
- poj2019 Cornfields RMQ
- 第三次JAVA基础考试总结
- Android Dialog使用、自定义带动画的Dialog使用
- 事件委托
- mysql5.7.17 centos6.4x86 source安装
- code1018 单词接龙 dfs,字符串处理
- 清明杂记
- 猴子分桃
- 【Tarjan】【SCC】【拓扑排序】Graph practice T3 graph 题解
- 拓扑排序——Codeforces Round #290 (Div. 2) C. Fox And Names
- 双显卡(Intel+Nvidia)笔记本配置cuda开发环境