AlertDialog

  • 默认样式
  • 单选样式
  • 多选样式
  • 自定义样式

效果图

 
AlertDialog效果图
  1. class OnClick implements View.OnClickListener {
  2. @Override
  3. public void onClick(View v) {
  4. switch (v.getId()) {
  5. case R.id.btn_dialog1:
  6. AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
  7. builder.setTitle("请回答").setMessage("你觉得课程如何")
  8. .setIcon(R.drawable.user)
  9. .setPositiveButton("棒", new DialogInterface.OnClickListener() {
  10. @Override
  11. public void onClick(DialogInterface dialog, int which) {
  12. ToastUtil.showShort(DialogActivity.this, "很诚实");
  13. }
  14. }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
  15. @Override
  16. public void onClick(DialogInterface dialog, int which) {
  17. ToastUtil.showShort(DialogActivity.this, "再瞅瞅");
  18. }
  19. }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
  20. @Override
  21. public void onClick(DialogInterface dialog, int which) {
  22. ToastUtil.showShort(DialogActivity.this, "睁眼说瞎话");
  23. }
  24. }).show();
  25. break;
  26. case R.id.btn_dialog2:
  27. final String[] array = new String[]{"男", "女"};
  28. AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
  29. builder1.setTitle("选择性别").setItems(array, new DialogInterface.OnClickListener() {
  30. @Override
  31. public void onClick(DialogInterface dialog, int which) {
  32. ToastUtil.showLong(DialogActivity.this, array[which]);
  33. }
  34. }).show();
  35. break;
  36. case R.id.btn_dialog3:
  37. final String[] array3 = new String[]{"男", "女"};
  38. AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
  39. builder3.setTitle("选择性别").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
  40. @Override
  41. public void onClick(DialogInterface dialog, int which) {
  42. ToastUtil.showLong(DialogActivity.this, array3[which]);
  43. dialog.dismiss();
  44. }
  45. }).setCancelable(false).show();
  46. break;
  47. case R.id.btn_dialog4:
  48. final String[] array4 = new String[]{"唱歌", "跳舞", "写代码"};
  49. boolean[] isSelected = new boolean[]{false, false, false};
  50. AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
  51. builder4.setTitle("选择兴趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
  52. @Override
  53. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  54. ToastUtil.showLong(DialogActivity.this, array4[which] + ":" + isChecked);
  55. }
  56. }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
  57. @Override
  58. public void onClick(DialogInterface dialog, int which) {
  59. }
  60. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int which) {
  63. }
  64. }).show();
  65. break;
  66. case R.id.btn_dialog5:
  67. AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
  68. View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog, null);
  69. EditText etUserName = view.findViewById(R.id.et_username);
  70. EditText etPassWord = view.findViewById(R.id.et_password);
  71. Button button = view.findViewById(R.id.et_login);
  72. button.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View v) {
  75. }
  76. });
  77. builder5.setTitle("请先登录").setView(view).show();
  78. break;
  79. }
  80. }
  81. }

自定义样式layout

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical" android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <EditText
  5. android:id="@+id/et_username"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:hint="username"
  9. android:maxLines="1"/>
  10. <EditText
  11. android:id="@+id/et_password"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:hint="password"
  15. android:inputType="textPassword"
  16. android:maxLines="1"/>
  17. <Button
  18. android:id="@+id/et_login"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="login"
  22. android:textAllCaps="false"
  23. android:layout_marginTop="10dp"/>
  24. </LinearLayout>

Progress

 
Progress效果图

代码

activity_progress.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".ProgressActivity"
  7. android:orientation="vertical"
  8. android:gravity="center_horizontal">
  9. <ProgressBar
  10. android:id="@+id/pb1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"/>
  13. <ProgressBar
  14. android:id="@+id/pb2"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. style="@android:style/Widget.ProgressBar"/>
  18. <ProgressBar
  19. android:id="@+id/pb3"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. style="@android:style/Widget.ProgressBar.Horizontal"
  23. android:progress="40"
  24. android:max="200"
  25. android:secondaryProgress="80"
  26. />
  27. <ProgressBar
  28. android:id="@+id/pb4"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. style="@android:style/Widget.Material.ProgressBar.Horizontal"
  32. android:progress="40"
  33. android:max="200"
  34. android:secondaryProgress="80"
  35. />
  36. <Button
  37. android:id="@+id/btn"
  38. android:layout_width="match_parent"
  39. android:layout_height="40dp"
  40. android:text="点击"/>
  41. <ProgressBar
  42. android:id="@+id/pb5"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. style="@style/MyProgressBar"
  46. android:layout_marginTop="10dp"
  47. />
  48. <!-- android:indeterminateDrawable="@drawable/bg_progress"-->
  49. <Button
  50. android:id="@+id/btn_progress_dialog1"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:textAllCaps="false"
  54. android:layout_marginTop="10dp"
  55. android:text="ProgressDialog1"/>
  56. <Button
  57. android:id="@+id/btn_progress_dialog2"
  58. android:layout_width="match_parent"
  59. android:layout_height="wrap_content"
  60. android:textAllCaps="false"
  61. android:layout_marginTop="10dp"
  62. android:text="ProgressDialog2"/>
  63. </LinearLayout>

ProgressActivity

  1. public class ProgressActivity extends AppCompatActivity {
  2. private ProgressBar mPb3;
  3. private Button pbBtn, mBtnProgressDialog1, mBtnProgressDialog2;
  4. @SuppressLint("WrongViewCast")
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_progress);
  9. mPb3 = findViewById(R.id.pb3);
  10. mPb3.setProgress(90);
  11. mBtnProgressDialog1 = findViewById(R.id.btn_progress_dialog1);
  12. mBtnProgressDialog2 = findViewById(R.id.btn_progress_dialog2);
  13. pbBtn = findViewById(R.id.btn);
  14. pbBtn.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. handler.sendEmptyMessage(0);
  18. }
  19. });
  20. mBtnProgressDialog1.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
  24. progressDialog.setTitle("提示");
  25. progressDialog.setMessage("正在加载");
  26. progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  27. @Override
  28. public void onCancel(DialogInterface dialog) {
  29. ToastUtil.showLong(ProgressActivity.this, "cancel");
  30. }
  31. });
  32. progressDialog.setCancelable(false);
  33. progressDialog.show();
  34. }
  35. });
  36. mBtnProgressDialog2.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
  40. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  41. progressDialog.setTitle("提示");
  42. progressDialog.setMessage("正在下载...");
  43. progressDialog.setButton(BUTTON_POSITIVE, "棒", new DialogInterface.OnClickListener() {
  44. @Override
  45. public void onClick(DialogInterface dialog, int which) {
  46. }
  47. });
  48. progressDialog.show();
  49. }
  50. });
  51. }
  52. @SuppressLint("HandlerLeak")
  53. Handler handler = new Handler() {
  54. @Override
  55. public void handleMessage(Message msg) {
  56. super.handleMessage(msg);
  57. if (mPb3.getProgress() < 200) {
  58. handler.postDelayed(runnable, 500);
  59. }else {
  60. ToastUtil.showShort(ProgressActivity.this, "加载完成");
  61. }
  62. }
  63. };
  64. Runnable runnable = new Runnable() {
  65. @Override
  66. public void run() {
  67. mPb3.setProgress(mPb3.getProgress() + 5);
  68. handler.sendEmptyMessage(0);
  69. }
  70. };
  71. }

自定义Dialog

 
自定义Dialog

layout_custom_dialog.xml


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical" android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:gravity="center_horizontal"
  5. android:background="@drawable/bg_custom_dialog"
  6. >
  7. <TextView
  8. android:id="@+id/tv_title"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:textSize="20sp"
  12. android:textColor="@color/colorBlack"
  13. android:text="提示"
  14. android:textStyle="bold"
  15. android:layout_marginTop="20dp"
  16. />
  17. <TextView
  18. android:id="@+id/tv_message"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:textSize="20sp"
  22. android:textColor="@color/colorBlack"
  23. android:text="删除?"
  24. android:layout_marginTop="20dp"
  25. android:layout_marginBottom="20dp"
  26. />
  27. <View
  28. android:layout_width="match_parent"
  29. android:layout_height="0.5dp"
  30. android:background="@color/colorGray"/>
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="45dp"
  34. android:orientation="horizontal">
  35. <TextView
  36. android:id="@+id/tv_cancel"
  37. android:layout_width="0dp"
  38. android:layout_height="match_parent"
  39. android:layout_weight="1"
  40. android:textSize="20sp"
  41. android:text="取消"
  42. android:textColor="#11c2ee"
  43. android:gravity="center"
  44. />
  45. <View
  46. android:layout_width="0.5dp"
  47. android:layout_height="match_parent"
  48. android:background="@color/colorGray"/>
  49. <TextView
  50. android:id="@+id/tv_confirm"
  51. android:layout_width="0dp"
  52. android:layout_height="match_parent"
  53. android:layout_weight="1"
  54. android:textSize="20sp"
  55. android:text="确定"
  56. android:textColor="#000"
  57. android:gravity="center"
  58. />
  59. </LinearLayout>
  60. </LinearLayout>

CustomDialogActivity

  1. public class CustomDialogActivity extends AppCompatActivity {
  2. private Button mBtnDialog;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_custom_dialog);
  7. mBtnDialog = findViewById(R.id.btn_custom_dialog);
  8. mBtnDialog.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View v) {
  11. CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this, R.style.CustomDialog);
  12. customDialog.setTitle("提示").setMessage("确认删除此项?").setCancel("取消",new CustomDialog.IOnCancelListener() {
  13. @Override
  14. public void onCancel(CustomDialog dialog) {
  15. }
  16. }).setConfirm("确定1",new CustomDialog.IOnConfirmListener() {
  17. @Override
  18. public void onConfirm(CustomDialog dialog) {
  19. }
  20. }).show();
  21. }
  22. });
  23. }
  24. }

activity_dialog_activity.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".CustomDialogActivity">
  7. <Button
  8. android:id="@+id/btn_custom_dialog"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="自定义dialog"
  12. android:textAllCaps="false"
  13. />
  14. </LinearLayout>

CustomDialog Style


  1. <style name="CustomDialog"
  2. parent="@android:style/Theme.Dialog">
  3. <item name="android:windowNoTitle">true</item>
  4. <item name="android:windowIsFloating">true</item>
  5. <item name="android:windowBackground">@color/colorAccent</item>
  6. </style>

Android开发-AlertDialog,Progress,ProgressDialog,自定义layout的更多相关文章

  1. [Android开发学iOS系列] Auto Layout

    [Android开发学iOS系列] Auto Layout 内容: 介绍什么是Auto Layout. 基本使用方法 在代码中写约束的方法 Auto Layout的原理 尺寸和优先级 Auto Lay ...

  2. Android开发学习之路-自定义ListView(继承BaseAdapter)

    大三学生一个,喜欢编程,喜欢谷歌,喜欢Android,所以选择的方向自然是Android应用开发,开博第一篇,希望以后会有更多的进步. 最近在做一个记账App的时候,需要一个Activity来显示每个 ...

  3. android 开发AlertDialog.builder对话框的实现

    AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...

  4. Android开发手记(8) ProgressDialog的使用

    ProgressDialog,进度对话框.一般有两种,一种是圆形的进度条(ProgressDialog.STYLE_SPINNER),另一种是长条形的进度条(ProgressDialog.STYLE_ ...

  5. Android之alertDialog、ProgressDialog

    一.alertDialog 置顶于所有控件之上的,可以屏蔽其他控件的交互能力.通过AlertDialog.Builder创建一个AlertDialog,并通过setTittle(),setMesseg ...

  6. Android开发(二)——自定义圆形控件的使用CircleImageView

    CircleImageView,a fast circular ImageView perfect for profile images. 主要的类CircleImageView: package d ...

  7. Android开发AlertDialog解析

    打开源码,首先映入眼帘的是三个构造方法,但这三个构造方法都是protected类型的, 可见,不允许我们直接实例化AlertDialog. 因此,我们再看别的有没有方法.可以实例化 再仔细一看,发现一 ...

  8. android 开发 View _7_ 动态自定义View

    效果图: 代码: package com.example.lenovo.mydemo.myViewDemo; import android.content.Context; import androi ...

  9. android 开发 View _1_ View的子类们 和 视图坐标系图

    目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...

随机推荐

  1. Python-selenium显示等待

    #coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from seleni ...

  2. 【转】time 模块详解(时间获取和转换)

    转自鱼C论坛--https://fishc.com.cn/forum.php?mod=viewthread&tid=51326&highlight=time time 模块 -- 时间 ...

  3. poj1050 To the Max(降维dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49351   Accepted: 26142 Desc ...

  4. abstract关键字的说法

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  5. Codeforces Round #678 (Div. 2)

    Codeforces Round #678 (Div. 2) A. Reorder 题意:有一个有 n 个数的序列 a ,以及一个数 m ,问能否给序列a重新排序,能够满足式子 $\sum_{i=1} ...

  6. Lambda表达式(三)

    public class Test04 { public static void main(String[] args) { /* * Java8中,有一个新的类:Stream类型,它代表一个数据加工 ...

  7. buuctf-pwn:jarvisoj_level6_x64

    jarvisoj_level6_x64 只能申请unsorted bin大小下的unlink IDA看一下,可以发现edit里面有任意堆溢出的情况(realloc造成堆溢出) 然后free里面有UAF ...

  8. 系统日志报错i8042prt无法加载

    原因如下: 解决方法为: 此报错可以直接忽略,不过由此可能导致即插即用(plugplay)报错,在即插即用报错时,重启服务器即可.

  9. [LuoguP1005]矩阵取数游戏 (DP+高精度)

    题面 传送门:https://www.luogu.org/problemnew/show/P1005 Solution 我们可以先考虑贪心 我们每一次都选左右两边尽可能小的数,方便大的放在后面 听起来 ...

  10. Cocos Creator 源码解读:引擎启动与主循环

    前言 预备 不知道你有没有想过,假如把游戏世界比作一辆汽车,那么这辆"汽车"是如何启动,又是如何持续运转的呢? 如题,本文的内容主要为 Cocos Creator 引擎的启动流程和 ...