mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-15 05:52:09 +08:00
任务列表顶部添加搜索框,支持中文/拼音/拼音首字母搜索
- 新增搜索框 EditText,输入时实时过滤任务列表 - 引入 pinyin4j 库,支持中文全拼和首字母搜索 - matchesSearch 匹配任务名称:中文原文 / 全拼 / 首字母 - 抽取 applyChanges() 统一排序、过滤、保存逻辑 - Adapter 新增 setData() 方法支持动态更新数据
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Fri May 22 19:00:55 HKT 2026
|
||||
#Fri May 22 19:31:43 HKT 2026
|
||||
stageCount=6
|
||||
libraryProject=
|
||||
baseVersion=15.20
|
||||
publishVersion=15.20.5
|
||||
buildCount=0
|
||||
buildCount=1
|
||||
baseBetaVersion=15.20.6
|
||||
|
||||
+117
-12
@@ -2,6 +2,8 @@ package cc.winboll.studio.stallhelper.activities;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
@@ -25,6 +27,11 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen
|
||||
* @Date 2026/05/18
|
||||
@@ -41,6 +48,9 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
|
||||
ArrayList<TaskSchedulerBean> mTaskList;
|
||||
TaskSchedulerAdapter mAdapter;
|
||||
EditText mSearchEditText;
|
||||
String mSearchQuery = "";
|
||||
HanyuPinyinOutputFormat mPinyinFormat;
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
@@ -61,6 +71,28 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
mTaskList = new ArrayList<TaskSchedulerBean>();
|
||||
loadTaskData();
|
||||
|
||||
// 设置搜索框
|
||||
mPinyinFormat = new HanyuPinyinOutputFormat();
|
||||
mPinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
mPinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
|
||||
mSearchEditText = findViewById(R.id.et_search);
|
||||
mSearchEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
mSearchQuery = s.toString().trim().toLowerCase();
|
||||
filterTaskList();
|
||||
}
|
||||
});
|
||||
|
||||
// 设置 RecyclerView
|
||||
RecyclerView recyclerView = findViewById(R.id.task_scheduler_recycler_view);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
|
||||
@@ -98,6 +130,12 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
sortTaskList();
|
||||
}
|
||||
|
||||
private void applyChanges() {
|
||||
sortTaskList();
|
||||
filterTaskList();
|
||||
saveTaskData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存任务数据到文件
|
||||
*/
|
||||
@@ -196,6 +234,76 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
mTaskList.add(new TaskSchedulerBean("烹饪晚餐", "制作丰盛的晚餐", time17, 90));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将中文转换为拼音(不含声调,小写)
|
||||
*/
|
||||
private String toPinyin(String chinese) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < chinese.length(); i++) {
|
||||
char c = chinese.charAt(i);
|
||||
try {
|
||||
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c, mPinyinFormat);
|
||||
if (pinyins != null && pinyins.length > 0) {
|
||||
sb.append(pinyins[0]);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将中文转换为拼音首字母串(小写)
|
||||
*/
|
||||
private String toPinyinInitials(String chinese) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < chinese.length(); i++) {
|
||||
char c = chinese.charAt(i);
|
||||
try {
|
||||
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c, mPinyinFormat);
|
||||
if (pinyins != null && pinyins.length > 0) {
|
||||
sb.append(pinyins[0].charAt(0));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查任务名称是否匹配搜索关键词(支持中文/拼音/首字母)
|
||||
*/
|
||||
private boolean matchesSearch(TaskSchedulerBean task, String query) {
|
||||
if (query == null || query.isEmpty()) return true;
|
||||
String name = task.getTaskName();
|
||||
if (name == null) return false;
|
||||
if (name.toLowerCase().contains(query)) return true;
|
||||
String pinyin = toPinyin(name);
|
||||
if (pinyin.contains(query)) return true;
|
||||
String initials = toPinyinInitials(name);
|
||||
if (initials.contains(query)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据搜索关键词过滤任务列表并刷新界面
|
||||
*/
|
||||
private void filterTaskList() {
|
||||
if (mSearchQuery.isEmpty()) {
|
||||
mAdapter.setData(mTaskList);
|
||||
return;
|
||||
}
|
||||
ArrayList<TaskSchedulerBean> filteredList = new ArrayList<TaskSchedulerBean>();
|
||||
for (TaskSchedulerBean task : mTaskList) {
|
||||
if (matchesSearch(task, mSearchQuery)) {
|
||||
filteredList.add(task);
|
||||
}
|
||||
}
|
||||
mAdapter.setData(filteredList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将月、天、小时转换为总分钟数
|
||||
* @param month 月份
|
||||
@@ -341,9 +449,7 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
task.setDurationMinutes(durationMinutes);
|
||||
task.setRemark(remark);
|
||||
|
||||
sortTaskList();
|
||||
mAdapter.notifyDataSetChanged();
|
||||
saveTaskData();
|
||||
applyChanges();
|
||||
LogUtils.i(TAG, "showEditTaskDialog: 任务 [" + taskName + "] 数据已更新,排档幅度=" + durationMinutes + "分钟");
|
||||
dialog.dismiss();
|
||||
}
|
||||
@@ -432,9 +538,7 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
int durationMinutes = convertToTotalMinutes(month, day, hour);
|
||||
TaskSchedulerBean newTask = new TaskSchedulerBean(taskName, remark, timestamp, durationMinutes);
|
||||
mTaskList.add(newTask);
|
||||
sortTaskList();
|
||||
mAdapter.notifyDataSetChanged();
|
||||
saveTaskData();
|
||||
applyChanges();
|
||||
LogUtils.i(TAG, "showAddTaskDialog: 任务 [" + taskName + "] 已添加,排档幅度=" + durationMinutes + "分钟");
|
||||
dialog.dismiss();
|
||||
}
|
||||
@@ -506,9 +610,7 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
public void onClick(View v) {
|
||||
String taskName = task.getTaskName();
|
||||
mTaskList.remove(position);
|
||||
sortTaskList();
|
||||
mAdapter.notifyDataSetChanged();
|
||||
saveTaskData();
|
||||
applyChanges();
|
||||
LogUtils.i(TAG, "showDeleteConfirmDialog: 任务 [" + taskName + "] 已删除");
|
||||
dialog.dismiss();
|
||||
}
|
||||
@@ -580,9 +682,7 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
long newTime = currentTime + duration * 60 * 1000L;
|
||||
LogUtils.i(TAG, "performSchedule: 当前时间 " + formatTimestamp(currentTime) + " + " + duration + "分钟 = " + formatTimestamp(newTime));
|
||||
currentTask.setCurrentScheduleTime(newTime);
|
||||
sortTaskList();
|
||||
mAdapter.notifyDataSetChanged();
|
||||
saveTaskData();
|
||||
applyChanges();
|
||||
LogUtils.i(TAG, "performSchedule: 更新任务 [" + currentTask.getTaskName() + "] 时间更新为 " + formatTimestamp(newTime));
|
||||
}
|
||||
|
||||
@@ -597,6 +697,11 @@ public class TaskSchedulerActivity extends WinBoLLActivity {
|
||||
mDataList = dataList;
|
||||
}
|
||||
|
||||
public void setData(ArrayList<TaskSchedulerBean> dataList) {
|
||||
mDataList = dataList;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mDataList.size();
|
||||
|
||||
@@ -16,6 +16,17 @@
|
||||
android:background="?attr/toolbarBackgroundColor"
|
||||
android:id="@+id/toolbar"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_search"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="搜索任务名称(支持中文/拼音)"
|
||||
android:padding="10dp"
|
||||
android:background="@android:drawable/edit_text"
|
||||
android:layout_margin="10dp"
|
||||
android:inputType="text"
|
||||
android:singleLine="true"/>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
|
||||
Reference in New Issue
Block a user