Skip to content

Commit

Permalink
feat: 优化点击按钮后收起键盘
Browse files Browse the repository at this point in the history
  • Loading branch information
liewstar committed Nov 28, 2024
1 parent d269cf7 commit 6c4a8eb
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions app/src/main/java/com/example/CQUPT/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import androidx.preference.PreferenceManager;

import com.example.CQUPT.databinding.ActivityMainBinding;
import com.example.CQUPT.utils.AppUsageInfo;
import com.example.CQUPT.utils.AppUsageManager;
import com.example.CQUPT.utils.UsageWarningDialog;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -125,21 +128,22 @@ private void checkAppUsageWithPermission() {
}

int timeThreshold = sharedPreferences.getInt(PREF_TIME_THRESHOLD, 30); // 默认30分钟
List<AppUsageInfo> excessiveUsageApps = new ArrayList<>();

for (String packageName : selectedApps) {
if (appUsageManager.isExcessiveUsage(packageName, timeThreshold)) {
String appName = getAppName(packageName);
String usageTime = appUsageManager.getFormattedUsageTime(
appUsageManager.getTodayAppUsageTime(packageName)
);

if (!isFinishing()) {
UsageWarningDialog dialog = new UsageWarningDialog(this, appName, usageTime);
dialog.show();
break; // 只显示一个提醒
}
excessiveUsageApps.add(new AppUsageInfo(appName, usageTime));
}
}

if (!excessiveUsageApps.isEmpty() && !isFinishing()) {
UsageWarningDialog dialog = new UsageWarningDialog(this, excessiveUsageApps);
dialog.show();
}
} catch (Exception e) {
android.util.Log.e("MainActivity", "Error in checkAppUsageWithPermission", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.example.CQUPT.adapter.CourseSuccessAdapter;
import com.example.CQUPT.databinding.FragmentCourseSelectionBinding;
import com.example.CQUPT.utils.KeyboardUtils;
import com.google.android.material.button.MaterialButton;

import java.util.Arrays;
Expand Down Expand Up @@ -77,6 +78,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {}
}

private void startCourseSelection() {
// 收起键盘
KeyboardUtils.hideKeyboard(requireActivity());

// 从SharedPreferences获取最新的Session
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
String session = prefs.getString(PREF_SESSION_ID, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.example.CQUPT.R;
import com.example.CQUPT.databinding.FragmentLoginBinding;
import com.example.CQUPT.ui.HttpUtil;
import com.example.CQUPT.utils.KeyboardUtils;
import com.google.android.material.chip.Chip;
import com.google.android.material.snackbar.Snackbar;

Expand Down Expand Up @@ -61,6 +62,9 @@ private void loadSavedData() {
}

private void handleLogin() {
// 收起键盘
KeyboardUtils.hideKeyboard(requireActivity());

String username = binding.usernameInput.getText().toString();
String password = binding.passwordInput.getText().toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.lifecycle.ViewModelProvider;
import com.example.CQUPT.databinding.FragmentLoadClassesBinding;
import com.example.CQUPT.ui.HttpUtil;
import com.example.CQUPT.utils.KeyboardUtils;
import com.google.android.material.snackbar.Snackbar;

import java.io.File;
Expand Down Expand Up @@ -43,6 +44,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
}

private void importCalendar() {
KeyboardUtils.hideKeyboard(requireActivity());
String studentId = binding.studentIdInput.getText().toString();
if (studentId.isEmpty()) {
showMessage("请输入学号");
Expand All @@ -63,6 +65,7 @@ private void importCalendar() {
}

private void shareCalendar() {
KeyboardUtils.hideKeyboard(requireActivity());
String studentId = binding.studentIdInput.getText().toString();
if (studentId.isEmpty()) {
showMessage("请输入学号");
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/CQUPT/utils/AppUsageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.CQUPT.utils;

public class AppUsageInfo {
private final String appName;
private final String usageTime;

public AppUsageInfo(String appName, String usageTime) {
this.appName = appName;
this.usageTime = usageTime;
}

public String getAppName() {
return appName;
}

public String getUsageTime() {
return usageTime;
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/com/example/CQUPT/utils/KeyboardUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.CQUPT.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class KeyboardUtils {
public static void hideKeyboard(Activity activity) {
if (activity == null) return;

// 获取当前焦点的View
View currentFocus = activity.getCurrentFocus();
if (currentFocus != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
// 清除焦点
currentFocus.clearFocus();
}
}

public static void hideKeyboard(View view) {
if (view == null) return;

InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
// 清除焦点
view.clearFocus();
}
}
30 changes: 28 additions & 2 deletions app/src/main/java/com/example/CQUPT/utils/UsageWarningDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -14,9 +17,11 @@

import com.example.CQUPT.R;

import java.util.List;

public class UsageWarningDialog extends Dialog {

public UsageWarningDialog(Context context, String appName, String usageTime) {
public UsageWarningDialog(Context context, List<AppUsageInfo> appUsageInfoList) {
super(context);

requestWindowFeature(Window.FEATURE_NO_TITLE);
Expand All @@ -34,7 +39,28 @@ public UsageWarningDialog(Context context, String appName, String usageTime) {
}

TextView messageText = view.findViewById(R.id.message_text);
messageText.setText(String.format("今天已经使用%s %s了,\n要不要休息一下?", appName, usageTime));
SpannableStringBuilder message = new SpannableStringBuilder();
message.append("今天以下应用的使用时间已超过设定阈值:\n\n");

for (int i = 0; i < appUsageInfoList.size(); i++) {
AppUsageInfo info = appUsageInfoList.get(i);

// 应用名加粗
int start = message.length();
message.append(info.getAppName());
message.setSpan(new StyleSpan(Typeface.BOLD), start, message.length(), 0);

message.append(":");
message.append(info.getUsageTime());

// 如果不是最后一个应用,添加换行
if (i < appUsageInfoList.size() - 1) {
message.append("\n");
}
}

message.append("\n\n要不要休息一下?");
messageText.setText(message);

Button okButton = view.findViewById(R.id.ok_button);
okButton.setOnClickListener(v -> dismiss());
Expand Down

0 comments on commit 6c4a8eb

Please sign in to comment.