当前位置:   article > 正文

学习Android的第二十一天

学习Android的第二十一天

目录

Android ProgressDialog (进度条对话框)

例子

Android DatePickerDialog 日期选择对话框

例子

Android TimePickerDialog 时间选择对话框

Android PopupWindow 悬浮框

构造函数

方法

例子

官方文档

Android OptionMenu 选项菜单

例子

官方文档


Android ProgressDialog (进度条对话框)

Android ProgressDialog 已经在 API 26+ 中被废弃,ProgressDialog 不能直接从 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. xmlns:tools="http://schemas.android.com/tools"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn_show_normal_progress_dialog"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="显示普通ProgressDialog" />
  14. <Button
  15. android:id="@+id/btn_show_indeterminate_progress_dialog"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="显示不确定结束的ProgressDialog" />
  19. <Button
  20. android:id="@+id/btn_show_determinate_progress_dialog"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="显示确定结束的ProgressDialog" />
  24. </LinearLayout>
  1. package com.example.myapplication;
  2. import android.app.ProgressDialog;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity {
  9. private ProgressDialog progressDialog;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Button btnShowNormal = findViewById(R.id.btn_show_normal_progress_dialog);
  15. Button btnShowIndeterminate = findViewById(R.id.btn_show_indeterminate_progress_dialog);
  16. Button btnShowDeterminate = findViewById(R.id.btn_show_determinate_progress_dialog);
  17. btnShowNormal.setOnClickListener(v -> {
  18. showProgressDialog(ProgressDialog.STYLE_SPINNER); // 普通ProgressDialog
  19. });
  20. btnShowIndeterminate.setOnClickListener(v -> {
  21. showProgressDialog(ProgressDialog.STYLE_HORIZONTAL); // 不确定结束的ProgressDialog
  22. });
  23. btnShowDeterminate.setOnClickListener(v -> {
  24. showDeterminateProgressDialog(); // 确定结束的ProgressDialog
  25. });
  26. }
  27. private void showProgressDialog(int style) {
  28. progressDialog = new ProgressDialog(MainActivity.this);
  29. progressDialog.setTitle("软件更新中");
  30. progressDialog.setMessage("软件正在更新中,请稍后...");
  31. progressDialog.setProgressStyle(style);
  32. progressDialog.setCancelable(false); // 防止用户取消
  33. progressDialog.show();
  34. // 模拟操作
  35. new Handler().postDelayed(() -> {
  36. progressDialog.dismiss();
  37. }, 3000); // 模拟3秒后关闭对话框
  38. }
  39. private void showDeterminateProgressDialog() {
  40. progressDialog = new ProgressDialog(MainActivity.this);
  41. progressDialog.setTitle("软件更新中");
  42. progressDialog.setMessage("软件正在更新中,请稍后...");
  43. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  44. progressDialog.setMax(100); // 设置最大值
  45. progressDialog.setCancelable(false);
  46. progressDialog.show();
  47. // 模拟下载进度更新
  48. new Thread(() -> {
  49. int progress = 0;
  50. while (progress <= 100) {
  51. progressDialog.setProgress(progress);
  52. progress += 10;
  53. try {
  54. Thread.sleep(500); // 模拟下载延迟
  55. } catch (InterruptedException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. progressDialog.dismiss();
  60. }).start();
  61. }
  62. }

Android DatePickerDialog 日期选择对话框

当用户点击一个按钮,弹出日期选择对话框是一个常见的需求。

例子

  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. xmlns:tools="http://schemas.android.com/tools"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn_pick_date"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="选择日期"/>
  14. </LinearLayout>
  1. package com.example.myapplication;
  2. import android.app.DatePickerDialog;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import java.util.Calendar;
  7. public class MainActivity extends AppCompatActivity {
  8. private Button btnPickDate;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. btnPickDate = findViewById(R.id.btn_pick_date);
  14. btnPickDate.setOnClickListener(v -> showDatePickerDialog());
  15. }
  16. private void showDatePickerDialog() {
  17. // 获取当前日期
  18. Calendar calendar = Calendar.getInstance();
  19. int year = calendar.get(Calendar.YEAR);
  20. int month = calendar.get(Calendar.MONTH);
  21. int day = calendar.get(Calendar.DAY_OF_MONTH);
  22. // 创建并显示日期选择对话框
  23. DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, selectedYear, selectedMonth, selectedDay) -> {
  24. // 处理选择的日期
  25. String selectedDate = selectedYear + "-" + (selectedMonth + 1) + "-" + selectedDay;
  26. btnPickDate.setText(selectedDate); // 在按钮上显示选择的日期
  27. }, year, month, day);
  28. datePickerDialog.show();
  29. }
  30. }

Android TimePickerDialog 时间选择对话框

Android TimePickerDialog ( 时间选择对话框 ) 会弹出一个对话框形式的时间选择器。

  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. xmlns:tools="http://schemas.android.com/tools"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/btn_pick_time"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="选择时间"/>
  14. </LinearLayout>
  1. package com.example.myapplication;
  2. import android.app.TimePickerDialog;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import java.util.Calendar;
  7. import java.util.TimeZone;
  8. public class MainActivity extends AppCompatActivity {
  9. private Button btnPickTime;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. btnPickTime = findViewById(R.id.btn_pick_time);
  15. btnPickTime.setOnClickListener(v -> showTimePickerDialog());
  16. }
  17. private void showTimePickerDialog() {
  18. // 获取中国的当前时间
  19. Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 设置时区为中国标准时间
  20. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  21. int minute = calendar.get(Calendar.MINUTE);
  22. // 创建并显示时间选择对话框
  23. TimePickerDialog timePickerDialog = new TimePickerDialog(this, (view, selectedHour, selectedMinute) -> {
  24. // 处理选择的时间
  25. String selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute);
  26. btnPickTime.setText(selectedTime); // 在按钮上显示选择的时间
  27. }, hour, minute, true); // 最后一个参数表示是否为24小时制
  28. timePickerDialog.show();
  29. }
  30. }

Android PopupWindow 悬浮框

PopupWindow 是一个弹出窗口控件,可以显示任意 View,并且浮动在当前 Activity 的顶部。通常用于悬浮在其他 UI 控件旁边,提供额外的信息或操作选项。与 AlertDialog 不同,PopupWindow 的位置可以自由设置,可以根据需要显示在屏幕的任何位置。

构造函数

PopupWindow 类的构造函数如下:

  1. PopupWindow(): 创建一个空的 PopupWindow 对象。
  2. PopupWindow(Context context): 使用指定的上下文创建 PopupWindow 对象。
  3. PopupWindow(Context context, AttributeSet attrs): 使用指定的上下文和属性集创建 PopupWindow 对象。
  4. PopupWindow(Context context, AttributeSet attrs, int defStyleAttr): 使用指定的上下文、属性集和样式资源创建 PopupWindow 对象。
  5. PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes): 使用指定的上下文、属性集、样式资源和主题资源创建 PopupWindow 对象。
  6. PopupWindow(View contentView): 使用指定的内容视图创建 PopupWindow 对象。
  7. PopupWindow(int width, int height): 创建指定宽高的 PopupWindow 对象。
  8. PopupWindow(View contentView, int width, int height): 使用指定的内容视图以及宽高创建 PopupWindow 对象。
  9. PopupWindow(View contentView, int width, int height, boolean focusable): 使用指定的内容视图、宽高和焦点设置创建 PopupWindow 对象。

方法

以下是一些重要的 PopupWindow 方法的说明:

  1. setContentView(View contentView): 设置 PopupWindow 显示的 View。
  2. getContentView(): 获取 PopupWindow 显示的 View。
  3. showAsDropDown(View anchor): 在指定控件正下方显示 PopupWindow,无偏移。
  4. showAsDropDown(View anchor, int xoff, int yoff): 在指定控件位置显示 PopupWindow,并可以设置偏移量。
  5. showAtLocation(View parent, int gravity, int x, int y): 在父控件指定位置显示 PopupWindow,可以设置具体位置、偏移量等。需要传入 Activity 中的任意一个 View 作为 parent 参数。
  6. setWidth(int width) / setHeight(int height): 设置 PopupWindow 的宽度和高度,也可以在构造方法中指定。
  7. setFocusable(true): 设置 PopupWindow 是否具有焦点。当设置为 true 时,PopupWindow 弹出后会处理触摸事件和物理按键事件,必须先让 PopupWindow 消失才能响应其他事件。
  8. setAnimationStyle(int style): 设置 PopupWindow 弹出和消失的动画效果。

例子

1、创建 res/layout/popup_content.xml 布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:padding="16dp">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="弹出内容"
  11. android:textSize="16sp"
  12. android:textColor="@android:color/black" />
  13. <Button
  14. android:id="@+id/close_button"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="关闭"
  18. android:textColor="@android:color/white"
  19. android:background="@color/purple" />
  20. </LinearLayout>

2、修改 res/layout/activity_main.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. xmlns:tools="http://schemas.android.com/tools"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/show_popup_button"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="显示弹出菜单" />
  14. </LinearLayout>

3、修改 MainActivity.java:

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.Button;
  7. import android.widget.PopupWindow;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. public class MainActivity extends AppCompatActivity {
  10. private PopupWindow popupWindow;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. // 创建一个 View 作为 PopupWindow 的内容视图
  16. View contentView = LayoutInflater.from(this).inflate(R.layout.popup_content, null);
  17. // 创建 PopupWindow 对象并设置内容视图、宽高
  18. popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  19. // 设置焦点,让 PopupWindow 弹出后处理触摸事件和物理按键事件
  20. popupWindow.setFocusable(true);
  21. // 找到触发弹出 PopupWindow 的按钮
  22. Button showPopupButton = findViewById(R.id.show_popup_button);
  23. showPopupButton.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. // 在按钮下方显示 PopupWindow,无偏移
  27. popupWindow.showAsDropDown(v);
  28. }
  29. });
  30. // 找到关闭 PopupWindow 的按钮
  31. Button closeButton = contentView.findViewById(R.id.close_button);
  32. closeButton.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. // 关闭 PopupWindow
  36. popupWindow.dismiss();
  37. }
  38. });
  39. }
  40. }

官方文档

  1. Android PopupWindow

Android OptionMenu 选项菜单

在Android中,OptionMenu(选项菜单)的创建和管理不是通过XML直接实例化或某个类的实例化方法实现的,而是通过Activity提供的几个生命周期方法进行动态创建和控制。

具体来说:

  • onCreateOptionsMenu(Menu menu):这是用于初始化选项菜单的方法,在这个方法里,我们可以调用getMenuInflater().inflate(R.menu.menu_main, menu);来加载预定义在XML文件中的菜单项,或者通过调用menu对象的add()方法动态添加菜单项。
  • onOptionsItemSelected(MenuItem item):当用户点击选项菜单中的某一项时,系统会调用此方法,我们可以在其中根据item的id进行相应的操作处理。
  • onPrepareOptionsMenu(Menu menu):在每次菜单即将显示前,系统都会调用此方法,以便我们可以根据当前应用的状态动态修改菜单项的状态或可见性等属性。
  • onOptionsMenuClosed(Menu menu):当选项菜单关闭后,系统会调用此回调方法,可以在此处执行菜单关闭后的清理工作。
  • onMenuOpened(int featureId, Menu menu):如果我们的应用支持旧版的API(如ActionBar的子菜单),当选项菜单打开时,系统可能会调用此方法通知我们菜单已打开。

例子

1、在 res/menu/menu_main.xml 菜单文件中定义要显示的菜单项:

  1. <!-- menu_main.xml -->
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/menu_item1"
  5. android:title="选项 1" />
  6. <item
  7. android:id="@+id/menu_item2"
  8. android:title="选项 2" />
  9. </menu>

2、修改 res/layout/activity_main.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. xmlns:tools="http://schemas.android.com/tools"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/open_popup_menu_button"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="显示弹出菜单" />
  14. </LinearLayout>

3、修改 MainActivity.java:

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.MenuItem;
  4. import android.widget.Button;
  5. import android.widget.PopupMenu;
  6. import android.widget.Toast;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. Button openPopupMenuButton = findViewById(R.id.open_popup_menu_button);
  14. openPopupMenuButton.setOnClickListener(v -> {
  15. PopupMenu popupMenu = new PopupMenu(MainActivity.this, openPopupMenuButton);
  16. popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu());
  17. popupMenu.setOnMenuItemClickListener(item -> {
  18. int id = item.getItemId();
  19. if (id == R.id.menu_item1) {
  20. // 处理选中 选项 1 的事件
  21. Toast.makeText(MainActivity.this, "选择了选项1", Toast.LENGTH_SHORT).show();
  22. return true;
  23. } else if (id == R.id.menu_item2) {
  24. // 处理选中 选项 2 的事件
  25. Toast.makeText(MainActivity.this, "选择了选项2", Toast.LENGTH_SHORT).show();
  26. return true;
  27. }
  28. return false;
  29. });
  30. popupMenu.show();
  31. });
  32. }
  33. }

官方文档

  1. Android menus

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/184496
推荐阅读
相关标签
  

闽ICP备14008679号