赞
踩
目录
Android ProgressDialog (进度条对话框)
Android DatePickerDialog 日期选择对话框
Android TimePickerDialog 时间选择对话框
Android ProgressDialog 已经在 API 26+ 中被废弃,ProgressDialog 不能直接从 XML 里创建。
- <?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"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/btn_show_normal_progress_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示普通ProgressDialog" />
-
- <Button
- android:id="@+id/btn_show_indeterminate_progress_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示不确定结束的ProgressDialog" />
-
- <Button
- android:id="@+id/btn_show_determinate_progress_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示确定结束的ProgressDialog" />
-
- </LinearLayout>
- package com.example.myapplication;
-
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.Button;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MainActivity extends AppCompatActivity {
-
- private ProgressDialog progressDialog;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btnShowNormal = findViewById(R.id.btn_show_normal_progress_dialog);
- Button btnShowIndeterminate = findViewById(R.id.btn_show_indeterminate_progress_dialog);
- Button btnShowDeterminate = findViewById(R.id.btn_show_determinate_progress_dialog);
-
- btnShowNormal.setOnClickListener(v -> {
- showProgressDialog(ProgressDialog.STYLE_SPINNER); // 普通ProgressDialog
- });
-
- btnShowIndeterminate.setOnClickListener(v -> {
- showProgressDialog(ProgressDialog.STYLE_HORIZONTAL); // 不确定结束的ProgressDialog
- });
-
- btnShowDeterminate.setOnClickListener(v -> {
- showDeterminateProgressDialog(); // 确定结束的ProgressDialog
- });
- }
-
- private void showProgressDialog(int style) {
- progressDialog = new ProgressDialog(MainActivity.this);
- progressDialog.setTitle("软件更新中");
- progressDialog.setMessage("软件正在更新中,请稍后...");
- progressDialog.setProgressStyle(style);
- progressDialog.setCancelable(false); // 防止用户取消
- progressDialog.show();
-
- // 模拟操作
- new Handler().postDelayed(() -> {
- progressDialog.dismiss();
- }, 3000); // 模拟3秒后关闭对话框
- }
-
- private void showDeterminateProgressDialog() {
- progressDialog = new ProgressDialog(MainActivity.this);
- progressDialog.setTitle("软件更新中");
- progressDialog.setMessage("软件正在更新中,请稍后...");
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setMax(100); // 设置最大值
- progressDialog.setCancelable(false);
- progressDialog.show();
-
- // 模拟下载进度更新
- new Thread(() -> {
- int progress = 0;
- while (progress <= 100) {
- progressDialog.setProgress(progress);
- progress += 10;
- try {
- Thread.sleep(500); // 模拟下载延迟
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- progressDialog.dismiss();
- }).start();
- }
- }
当用户点击一个按钮,弹出日期选择对话框是一个常见的需求。
- <?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"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/btn_pick_date"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="选择日期"/>
-
- </LinearLayout>
- package com.example.myapplication;
-
- import android.app.DatePickerDialog;
- import android.os.Bundle;
- import android.widget.Button;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import java.util.Calendar;
-
- public class MainActivity extends AppCompatActivity {
-
- private Button btnPickDate;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- btnPickDate = findViewById(R.id.btn_pick_date);
-
- btnPickDate.setOnClickListener(v -> showDatePickerDialog());
- }
-
- private void showDatePickerDialog() {
- // 获取当前日期
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH);
- int day = calendar.get(Calendar.DAY_OF_MONTH);
-
- // 创建并显示日期选择对话框
- DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, selectedYear, selectedMonth, selectedDay) -> {
- // 处理选择的日期
- String selectedDate = selectedYear + "-" + (selectedMonth + 1) + "-" + selectedDay;
- btnPickDate.setText(selectedDate); // 在按钮上显示选择的日期
- }, year, month, day);
- datePickerDialog.show();
- }
- }
Android TimePickerDialog ( 时间选择对话框 ) 会弹出一个对话框形式的时间选择器。
- <?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"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/btn_pick_time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="选择时间"/>
-
- </LinearLayout>
- package com.example.myapplication;
-
- import android.app.TimePickerDialog;
- import android.os.Bundle;
- import android.widget.Button;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import java.util.Calendar;
- import java.util.TimeZone;
-
- public class MainActivity extends AppCompatActivity {
-
- private Button btnPickTime;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- btnPickTime = findViewById(R.id.btn_pick_time);
-
- btnPickTime.setOnClickListener(v -> showTimePickerDialog());
- }
-
- private void showTimePickerDialog() {
- // 获取中国的当前时间
- Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 设置时区为中国标准时间
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- int minute = calendar.get(Calendar.MINUTE);
-
- // 创建并显示时间选择对话框
- TimePickerDialog timePickerDialog = new TimePickerDialog(this, (view, selectedHour, selectedMinute) -> {
- // 处理选择的时间
- String selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute);
- btnPickTime.setText(selectedTime); // 在按钮上显示选择的时间
- }, hour, minute, true); // 最后一个参数表示是否为24小时制
- timePickerDialog.show();
- }
- }
PopupWindow 是一个弹出窗口控件,可以显示任意 View,并且浮动在当前 Activity 的顶部。通常用于悬浮在其他 UI 控件旁边,提供额外的信息或操作选项。与 AlertDialog 不同,PopupWindow 的位置可以自由设置,可以根据需要显示在屏幕的任何位置。
PopupWindow 类的构造函数如下:
以下是一些重要的 PopupWindow 方法的说明:
1、创建 res/layout/popup_content.xml 布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:padding="16dp">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="弹出内容"
- android:textSize="16sp"
- android:textColor="@android:color/black" />
-
- <Button
- android:id="@+id/close_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="关闭"
- android:textColor="@android:color/white"
- android:background="@color/purple" />
- </LinearLayout>
2、修改 res/layout/activity_main.xml 布局文件:
- <?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"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/show_popup_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示弹出菜单" />
-
- </LinearLayout>
3、修改 MainActivity.java:
- package com.example.myapplication;
-
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.PopupWindow;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MainActivity extends AppCompatActivity {
-
- private PopupWindow popupWindow;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- // 创建一个 View 作为 PopupWindow 的内容视图
- View contentView = LayoutInflater.from(this).inflate(R.layout.popup_content, null);
-
- // 创建 PopupWindow 对象并设置内容视图、宽高
- popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
-
- // 设置焦点,让 PopupWindow 弹出后处理触摸事件和物理按键事件
- popupWindow.setFocusable(true);
-
- // 找到触发弹出 PopupWindow 的按钮
- Button showPopupButton = findViewById(R.id.show_popup_button);
- showPopupButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 在按钮下方显示 PopupWindow,无偏移
- popupWindow.showAsDropDown(v);
- }
- });
-
- // 找到关闭 PopupWindow 的按钮
- Button closeButton = contentView.findViewById(R.id.close_button);
- closeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 关闭 PopupWindow
- popupWindow.dismiss();
- }
- });
- }
- }
在Android中,OptionMenu(选项菜单)的创建和管理不是通过XML直接实例化或某个类的实例化方法实现的,而是通过Activity提供的几个生命周期方法进行动态创建和控制。
具体来说:
1、在 res/menu/menu_main.xml 菜单文件中定义要显示的菜单项:
- <!-- menu_main.xml -->
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/menu_item1"
- android:title="选项 1" />
- <item
- android:id="@+id/menu_item2"
- android:title="选项 2" />
- </menu>
2、修改 res/layout/activity_main.xml 布局文件:
- <?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"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/open_popup_menu_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示弹出菜单" />
-
- </LinearLayout>
3、修改 MainActivity.java:
- package com.example.myapplication;
-
- import android.os.Bundle;
- import android.view.MenuItem;
- import android.widget.Button;
- import android.widget.PopupMenu;
- import android.widget.Toast;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button openPopupMenuButton = findViewById(R.id.open_popup_menu_button);
- openPopupMenuButton.setOnClickListener(v -> {
- PopupMenu popupMenu = new PopupMenu(MainActivity.this, openPopupMenuButton);
- popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu());
-
- popupMenu.setOnMenuItemClickListener(item -> {
- int id = item.getItemId();
- if (id == R.id.menu_item1) {
- // 处理选中 选项 1 的事件
- Toast.makeText(MainActivity.this, "选择了选项1", Toast.LENGTH_SHORT).show();
- return true;
- } else if (id == R.id.menu_item2) {
- // 处理选中 选项 2 的事件
- Toast.makeText(MainActivity.this, "选择了选项2", Toast.LENGTH_SHORT).show();
- return true;
- }
- return false;
- });
-
- popupMenu.show();
- });
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。