经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android对话框AlertDialog详解
来源:jb51  时间:2021/12/17 11:53:27  对本文有异议

AlertDialog可以在当前的界面上显示一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般是用于提示一些非常重要的内容或者警告信息。

1.创建AlertDialog

首先,我们来了解一下AlertDialog的大体创建顺序。与TextView、Button这些控件稍有不同,AlertDialog并不是初始化(findViewById)之后就直接调用各种方法了。仔细想想AlertDialog的使用场景, 它并不像TextView和Button那些控件似的一般都是固定在界面上,而是在某个时机才会触发出来(比如用户点击了某个按钮或者断网了)。所以AlertDialog并不需要到布局文件中创建,而是在代码中通过构造器(AlertDialog.Builder)来构造标题、图标和按钮等内容的。

  • 1.创建构造器AlertDialog.Builder的对象;
  • 2.通过构造器对象调用setTitle、setMessage、setIcon等方法构造对话框的标题、信息和图标等内容;
  • 3.根据需要调用setPositive/Negative/NeutralButton()方法设置正面按钮、负面按钮和中立按钮;
  • 4.调用构造器对象的create方法创建AlertDialog对象;
  • 5.AlertDialog对象调用show方法,让对话框在界面上显示。

注:AlertDialog.Builder自己也有一个show方法,可以显示对话框,所以上面的第4、第5步可以简化为一步。

下面,我们就来创建几种常用的AlertDialog吧。新建一个工程,在activity_main.xml布局文件上放置五个按钮,点击按钮就会有相应的对话框弹出。

1.1 布局文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context="com.fd.alertdialog.MainActivity">
  8. <Button
  9. android:id="@+id/btn_normal_dialog"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="普通对话框" />
  13. <Button
  14. android:id="@+id/btn_item_dialog"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="普通列表对话框" />
  18. <Button
  19. android:id="@+id/btn_single_choice"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="单选对话框" />
  23. <Button
  24. android:id="@+id/btn_multi_choice"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="复选对话框" />
  28. <Button
  29. android:id="@+id/btn_custom_dialog"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="自定义对话框" />
  33. </LinearLayout>

1.2 MainActivity的主要代码如下所示:

  1. package com.fd.alertdialog;
  2. import android.content.DialogInterface;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  15. public static String TAG = MainActivity.class.getSimpleName();
  16. private int chedkedItem = 0;
  17. private String name;
  18. private String pwd;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. bindView();
  24. }
  25. private void bindView() {
  26. Button btn_normal_dialog = (Button) findViewById(R.id.btn_normal_dialog);
  27. Button btn_item_dialog = (Button) findViewById(R.id.btn_item_dialog);
  28. Button btn_single_choice = (Button) findViewById(R.id.btn_single_choice);
  29. Button btn_multi_choice = (Button) findViewById(R.id.btn_multi_choice);
  30. Button btn_custom_dialog = (Button) findViewById(R.id.btn_custom_dialog);
  31. btn_normal_dialog.setOnClickListener(this);
  32. btn_item_dialog.setOnClickListener(this);
  33. btn_single_choice.setOnClickListener(this);
  34. btn_multi_choice.setOnClickListener(this);
  35. btn_custom_dialog.setOnClickListener(this);
  36. }
  37. @Override
  38. public void onClick(View v) {
  39. switch (v.getId()) {
  40. case R.id.btn_normal_dialog:
  41. tipDialog(); //提示对话框
  42. break;
  43. case R.id.btn_item_dialog:
  44. itemListDialog(); //列表对话框
  45. break;
  46. case R.id.btn_single_choice:
  47. singleChoiceDialog(); //单选对话框
  48. break;
  49. case R.id.btn_multi_choice:
  50. multiChoiceDialog(); //多选对话框
  51. break;
  52. case R.id.btn_custom_dialog:
  53. customDialog(); //自定义对话框
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. }

代码比较简单,这里就不做详细讲解了。接下来看一下各个对话框的具体代码。

2.普通提示对话框

提示对话框应该是最常见的AlertDialog了,其上主要是提示标题,消息主体,底部“取消”、“确定”等按钮。

  1. /**
  2. * 提示对话框
  3. */
  4. public void tipDialog() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. builder.setTitle("提示:");
  7. builder.setMessage("这是一个普通对话框,");
  8. builder.setIcon(R.mipmap.ic_launcher);
  9. builder.setCancelable(true); //点击对话框以外的区域是否让对话框消失
  10. //设置正面按钮
  11. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  12. @Override
  13. public void onClick(DialogInterface dialog, int which) {
  14. Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
  15. dialog.dismiss();
  16. }
  17. });
  18. //设置反面按钮
  19. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  20. @Override
  21. public void onClick(DialogInterface dialog, int which) {
  22. Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
  23. dialog.dismiss();
  24. }
  25. });
  26. //设置中立按钮
  27. builder.setNeutralButton("保密", new DialogInterface.OnClickListener() {
  28. @Override
  29. public void onClick(DialogInterface dialog, int which) {
  30. Toast.makeText(MainActivity.this, "你选择了中立", Toast.LENGTH_SHORT).show();
  31. dialog.dismiss();
  32. }
  33. });
  34. AlertDialog dialog = builder.create(); //创建AlertDialog对象
  35. //对话框显示的监听事件
  36. dialog.setOnShowListener(new DialogInterface.OnShowListener() {
  37. @Override
  38. public void onShow(DialogInterface dialog) {
  39. Log.e(TAG, "对话框显示了");
  40. }
  41. });
  42. //对话框消失的监听事件
  43. dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  44. @Override
  45. public void onCancel(DialogInterface dialog) {
  46. Log.e(TAG, "对话框消失了");
  47. }
  48. });
  49. dialog.show(); //显示对话框
  50. }

具体介绍一下用到的方法吧:?

  • - setTitle:设置对话框的标题,比如“提示”、“警告”等;?
  • - setMessage:设置对话框要传达的具体信息;?
  • - setIcon: 设置对话框的图标;?
  • - setCancelable: 点击对话框以外的区域是否让对话框消失,默认为true;?
  • - setPositiveButton:设置正面按钮,表示“积极”、“确认”的意思,第一个参数为按钮上显示的文字,下同;?
  • - setNegativeButton:设置反面按钮,表示“消极”、“否认”、“取消”的意思;?
  • - setNeutralButton:设置中立按钮;?
  • - setOnShowListener:对话框显示时触发的事件;?
  • - setOnCancelListener:对话框消失时触发的事件。

当然,这些设置并不是非要不可,而是根据自己需要而定。比如标题、图标这些就可要可不要。

效果如下图所示:

你或许会有这样的疑问:既然底部那些按钮的文字和点击事件的内容都是我们自己来写的,那不是可以把正面按钮的内容和反面按钮的内容互换吗?看看运行后的效果图就会发现,反面按钮是在正面按钮的左边的,所以考虑到用户的操作习惯和代码的语义,我们最好还是按照API来写。

3.普通列表对话框

列表对话框的内容就是一列显示内容,需要用到构造器的setItems方法,参数一是列表数据,参数二是点击监听接口,我们要实现这样一个小功能,用户在点击某一项时弹出一个Toast提示选中项的内容。

代码如下所示:

  1. /**
  2. * 列表对话框
  3. */
  4. private void itemListDialog() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. builder.setTitle("选择你喜欢的课程:");
  7. builder.setCancelable(true);
  8. final String[] lesson = new String[]{"语文", "数学", "英语", "化学", "生物", "物理", "体育"};
  9. builder.setIcon(R.mipmap.ic_launcher);
  10. builder.setIcon(R.mipmap.tab_better_pressed)
  11. .setItems(lesson, new DialogInterface.OnClickListener() {
  12. @Override
  13. public void onClick(DialogInterface dialog, int which) {
  14. Toast.makeText(getApplicationContext(), "你选择了" + lesson[which], Toast.LENGTH_SHORT).show();
  15. }
  16. }).create();
  17. //设置正面按钮
  18. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  19. @Override
  20. public void onClick(DialogInterface dialog, int which) {
  21. dialog.dismiss();
  22. }
  23. });
  24. //设置反面按钮
  25. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  26. @Override
  27. public void onClick(DialogInterface dialog, int which) {
  28. dialog.dismiss();
  29. }
  30. });
  31. AlertDialog dialog = builder.create(); //创建AlertDialog对象
  32. dialog.show(); //显示对话框
  33. }

运行后的效果如下所示:

4.单选对话框

单选对话框的内容就是一个单项选择列表,需要用到setSingleChoiceItems方法,参数一是列表数据,参数二是默认选中的item,参数三则是点击监听接口,我们要实现这样一个小功能,用户在选好某一项之后记下其选择,下次点开对话框时就默认选中该项。

  1. /**
  2. * 单选对话框
  3. */
  4. public void singleChoiceDialog() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. builder.setTitle("你现在居住地是:");
  7. final String[] cities = {"北京", "上海", "广州", "深圳", "杭州", "天津", "成都"};
  8. builder.setSingleChoiceItems(cities, chedkedItem, new DialogInterface.OnClickListener() {
  9. @Override
  10. public void onClick(DialogInterface dialog, int which) {
  11. Toast.makeText(getApplicationContext(), "你选择了" + cities[which], Toast.LENGTH_SHORT).show();
  12. chedkedItem = which;
  13. }
  14. });
  15. builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
  16. @Override
  17. public void onClick(DialogInterface dialog, int which) {
  18. dialog.dismiss();
  19. }
  20. });
  21. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  22. @Override
  23. public void onClick(DialogInterface dialog, int which) {
  24. dialog.dismiss();
  25. }
  26. });
  27. AlertDialog dialog = builder.create(); //创建AlertDialog对象
  28. dialog.show(); //显示对话框
  29. }

运行后的效果如下所示:

你可能会把checkedItem的赋值放在确定按钮的点击事件中,这一看似乎没什么问题,但是这样是错误的!仔细阅读谷歌的API文档就知道了,setSingleChoiceItems 方法中实现的onClick方法中which表示的是当前选中的列表中的item下标,而setPositiveButton和setNegativeButton方法那里的which表示的却是按钮的种类,正面按钮中的which值是-1,反面按钮的是-2,与列表的item是没有关系的。

例子中的保存选中item的方法有问题的,当Activity被销毁之后重新创建的话数据就会丢失,要想持久化保存的话要用sharedpreferences或者数据库。

5.复选对话框

复选对话框是一个可以重复选中的列表,与单选对话框有点像,不过调用的是setMultiChoiceItems方法,而且多了一个布尔值参数isChecked,表示当前点击的item是否被选中。

我们创建一个集合,将点击选中的item添加到集合中,取消勾选的话就从集合中移除,点击确认按钮后就将选中内容显示出来。

  1. /**
  2. * 复选对话框
  3. */
  4. public void multiChoiceDialog() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. builder.setTitle("请选择你喜欢的颜色:");
  7. final String[] colors = {"红色", "橙色", "黄色", "绿色", "蓝色", "靛色", "紫色"};
  8. final List<String> myColors = new ArrayList<>();
  9. builder.setMultiChoiceItems(colors, null, new DialogInterface.OnMultiChoiceClickListener() {
  10. @Override
  11. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  12. if (isChecked) {
  13. myColors.add(colors[which]);
  14. } else {
  15. myColors.remove(colors[which]);
  16. }
  17. }
  18. });
  19. builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
  20. @Override
  21. public void onClick(DialogInterface dialog, int which) {
  22. String result = "";
  23. for (String color : myColors) {
  24. result += color + "、";
  25. }
  26. Toast.makeText(getApplicationContext(), "你选择了: " + result, Toast.LENGTH_SHORT).show();
  27. dialog.dismiss();
  28. }
  29. });
  30. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  31. @Override
  32. public void onClick(DialogInterface dialog, int which) {
  33. myColors.clear();
  34. dialog.dismiss();
  35. }
  36. });
  37. AlertDialog dialog = builder.create(); //创建AlertDialog对象
  38. dialog.show(); //显示对话框
  39. }

运行后效果图如下所示:

6.自定义登录对话框

有时候,只显示简单的标题和信息是满足不了我们的要求,比如我们要实现一个登录对话框的话,那就需要在对话框上放置EditText输入框了。AlertDialog早就为我们准备好了setView方法,只要往里面放进我们需要的对话框的View对象就可以了。

6.1自定义登录对话框的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/textView"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#169ee5"
  11. android:gravity="center"
  12. android:text="请先登录"
  13. android:textColor="@android:color/white"
  14. android:textSize="20sp" />
  15. <EditText
  16. android:id="@+id/et_name"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:hint="请输入你的账户名:"
  20. android:textSize="18sp" />
  21. <EditText
  22. android:id="@+id/et_pwd"
  23. android:inputType="textPassword"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:hint="请输入密码:"
  27. android:textSize="18sp" />
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_marginBottom="5dp"
  32. android:orientation="horizontal"
  33. android:paddingLeft="5dp"
  34. android:paddingRight="5dp">
  35. <Button
  36. android:id="@+id/btn_cancel"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_marginRight="10dp"
  40. android:layout_weight="1"
  41. android:background="#169ee5"
  42. android:text="取消"
  43. android:textColor="@android:color/white"
  44. android:textSize="16sp" />
  45. <Button
  46. android:id="@+id/btn_login"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1"
  50. android:background="#169ee5"
  51. android:text="登录"
  52. android:textColor="@android:color/white"
  53. android:textSize="16sp" />
  54. </LinearLayout>
  55. </LinearLayout>

6.2 自定义对话框的代码逻辑

setView方法是通过AlertDialog的对象调用的,所以这里的代码顺序会稍有不同:我们要先创建AlertDialog对象和View对象,然后再去初始化对话框中的控件。

  1. /**
  2. * 自定义登录对话框
  3. */
  4. public void customDialog() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. final AlertDialog dialog = builder.create();
  7. View dialogView = View.inflate(MainActivity.this, R.layout.activity_custom, null);
  8. dialog.setView(dialogView);
  9. dialog.show();
  10. final EditText et_name = dialogView.findViewById(R.id.et_name);
  11. final EditText et_pwd = dialogView.findViewById(R.id.et_pwd);
  12. final Button btn_login = dialogView.findViewById(R.id.btn_login);
  13. final Button btn_cancel = dialogView.findViewById(R.id.btn_cancel);
  14. btn_login.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17. name = et_name.getText().toString();
  18. pwd = et_pwd.getText().toString();
  19. if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
  20. Toast.makeText(MainActivity.this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
  21. return;
  22. }
  23. Toast.makeText(MainActivity.this, "用户名:" + name + "\n" + "用户密码:" + pwd, Toast.LENGTH_SHORT).show();
  24. dialog.dismiss();
  25. }
  26. });
  27. btn_cancel.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View view) {
  30. dialog.dismiss();
  31. }
  32. });
  33. }

运行后的效果图如下所示:

7.自定义对话框需要注意问题

7.1 系统dialog的宽度

默认是固定的,即使你自定义布局怎么修改宽度也不起作用,高度可根据布局自动调节。如果想修改弹出窗体大小,可以使用下面这段代码来实现改变对话框的宽高。这段代码必须在dialog.show()方法之后调用才有效。

  1. //此处设置位置窗体大小,
  2. dialog.getWindow().setLayout(width,height);

创建新的布局文件activity_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/textView"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#169ee5"
  11. android:gravity="center"
  12. android:text="请先登录"
  13. android:textColor="@android:color/white"
  14. android:textSize="20sp" />
  15. <TextView
  16. android:id="@+id/textView4"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:background="#169ee5"
  20. android:gravity="center"
  21. android:text="请先登录"
  22. android:textColor="@android:color/white"
  23. android:textSize="20sp" />
  24. <TextView
  25. android:id="@+id/textView3"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:background="#169ee5"
  29. android:gravity="center"
  30. android:text="请先登录"
  31. android:textColor="@android:color/white"
  32. android:textSize="20sp" />
  33. <TextView
  34. android:id="@+id/textView2"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:background="#169ee5"
  38. android:gravity="center"
  39. android:text="请先登录"
  40. android:textColor="@android:color/white"
  41. android:textSize="20sp" />
  42. <TextView
  43. android:id="@+id/textView1"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:background="#169ee5"
  47. android:gravity="center"
  48. android:text="请先登录"
  49. android:textColor="@android:color/white"
  50. android:textSize="20sp" />
  51. <EditText
  52. android:id="@+id/et_name"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:hint="请输入你的账户名:"
  56. android:textSize="18sp" />
  57. <EditText
  58. android:id="@+id/et_pwd"
  59. android:inputType="textPassword"
  60. android:layout_width="match_parent"
  61. android:layout_height="wrap_content"
  62. android:hint="请输入密码:"
  63. android:textSize="18sp" />
  64. <LinearLayout
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:layout_marginBottom="5dp"
  68. android:orientation="horizontal"
  69. android:paddingLeft="5dp"
  70. android:paddingRight="5dp">
  71. <Button
  72. android:id="@+id/btn_cancel"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:layout_marginRight="10dp"
  76. android:layout_weight="1"
  77. android:background="#169ee5"
  78. android:text="取消"
  79. android:textColor="@android:color/white"
  80. android:textSize="16sp" />
  81. <Button
  82. android:id="@+id/btn_login"
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:layout_weight="1"
  86. android:background="#169ee5"
  87. android:text="登录"
  88. android:textColor="@android:color/white"
  89. android:textSize="16sp" />
  90. </LinearLayout>
  91. </LinearLayout>

代码逻辑和6.2的代码逻辑差不多,只是多了设置对话框宽度的调用 。

  1. /**
  2. * 修改对话框显示的宽度
  3. */
  4. public void customDialogDisplay() {
  5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  6. final AlertDialog dialog = builder.create();
  7. View dialogView = View.inflate(MainActivity.this, R.layout.activity_layout, null);
  8. dialog.setView(dialogView);
  9. dialog.show();
  10. dialog.getWindow().setLayout(ScreenUtils.getScreenWidth(this)/4*3, LinearLayout.LayoutParams.WRAP_CONTENT);
  11. final EditText et_name = dialogView.findViewById(R.id.et_name);
  12. final EditText et_pwd = dialogView.findViewById(R.id.et_pwd);
  13. final Button btn_login = dialogView.findViewById(R.id.btn_login);
  14. final Button btn_cancel = dialogView.findViewById(R.id.btn_cancel);
  15. btn_login.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View view) {
  18. name = et_name.getText().toString();
  19. pwd = et_pwd.getText().toString();
  20. if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
  21. Toast.makeText(MainActivity.this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
  22. return;
  23. }
  24. Toast.makeText(MainActivity.this, "用户名:" + name + "\n" + "用户密码:" + pwd, Toast.LENGTH_SHORT).show();
  25. dialog.dismiss();
  26. }
  27. });
  28. btn_cancel.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View view) {
  31. dialog.dismiss();
  32. }
  33. });
  34. }

ScreenUtils工具类代码

  1. public class ScreenUtils {
  2. /**
  3. * 获取屏幕高度(px)
  4. */
  5. public static int getScreenHeight(Context context) {
  6. return context.getResources().getDisplayMetrics().heightPixels;
  7. }
  8. /**
  9. * 获取屏幕宽度(px)
  10. */
  11. public static int getScreenWidth(Context context) {
  12. return context.getResources().getDisplayMetrics().widthPixels;
  13. }
  14. }

效果图:

7.2 改变Android Dialog弹出后的Activity背景亮度:

在代码中修改.lp.alpha大小,值的大小可根据自己要求设置。

  1. // 设置屏幕背景变暗
  2. private void setScreenBgDarken() {
  3. WindowManager.LayoutParams lp = getWindow().getAttributes();
  4. lp.alpha = 0.5f;
  5. lp.dimAmount = 0.5f;
  6. getWindow().setAttributes(lp);
  7. }
  8. // 设置屏幕背景变亮
  9. private void setScreenBgLight() {
  10. WindowManager.LayoutParams lp = getWindow().getAttributes();
  11. lp.alpha = 1.0f;
  12. lp.dimAmount = 1.0f;
  13. getWindow().setAttributes(lp);
  14. }

7.3?如何控制弹窗弹出的位置:

一般都是在屏幕正中间弹出默认,但也可以控制从别的地方弹出,比如从底部弹出,可以这样写

  1. private void popFromBottom(Dialog dialog) {
  2. Window win = dialog.getWindow();
  3. win.setGravity(Gravity.BOTTOM); // 这里控制弹出的位置
  4. win.getDecorView().setPadding(0, 0, 0, 0);
  5. WindowManager.LayoutParams lp = win.getAttributes();
  6. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  7. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  8. dialog.getWindow().setBackgroundDrawable(null);
  9. win.setAttributes(lp);
  10. }

8.代码下载地址

http://xiazai.jb51.net/202112/yuanma/AlertDialogDemo_jb51.rar

到此这篇关于Android对话框AlertDialog详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号