|
|
发表于
2025-11-12 18:59:31
来自手机
|
显示全部楼层
来自 山东
本帖最后由 henkeai 于 2025-11-12 19:03 编辑
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.*;
- import android.text.SpannableString;
- import android.text.Spanned;
- import de.robv.android.xposed.IXposedHookLoadPackage;
- import de.robv.android.xposed.XC_MethodHook;
- import de.robv.android.xposed.XposedHelpers;
- import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- public class HookInit implements IXposedHookLoadPackage {
- @Override
- public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
- if (!lpparam.packageName.equals("com.fuli")) return;
- XposedHelpers.findAndHookMethod(
- "com.fuli.MainActivity",
- lpparam.classLoader,
- "onCreate",
- Bundle.class,
- new XC_MethodHook() {
- @Override
- protected void afterHookedMethod(MethodHookParam param) throws Throwable {
- // 修改
-
- Henkeai.henkeai(param.thisObject, "复利计算器", "1");
- Henkeai.henkeai(param.thisObject, "初始资金(元)", "2");
- Henkeai.henkeai(param.thisObject, "请输入初始资金", "3");
- Henkeai.henkeai(param.thisObject, "每日涨幅(%)", "4");
- Henkeai.henkeai(param.thisObject, "请输入每日涨幅百分比", "5");
- Henkeai.henkeai(param.thisObject, "目标资金(元)", "6");
- Henkeai.henkeai(param.thisObject, "请输入目标资金", "7");
- Henkeai.henkeai(param.thisObject, "计算天数", "8");
- Henkeai.henkeai(param.thisObject, "功能跳转", "9");
- Henkeai.henkeai(param.thisObject, "计算结果", "10");
- Henkeai.henkeai(param.thisObject, "计算结果", "11");
- Henkeai.henkeai(param.thisObject, "等待计算...", "12");
-
- // 批量修改
- // Henkeai.modifyMultipleText(param.thisObject,
- // new String[]{"a", "b", "c"},
- // new String[]{"a1", "b2", "c3"}
- // );
- }
- }
- );
- }
-
- // 修改Android控件
-
- public static class Henkeai {
-
- // 修改所有类型的文本
-
- public static void henkeai(Object activity, String findText, String newText) {
- try {
- View rootView = getRootView(activity);
- if (rootView instanceof ViewGroup) {
- traverseAndModifyAllText((ViewGroup) rootView, findText, newText);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- // 批量修改文本
-
- public static void modifyMultipleText(Object activity, String[] findTexts, String[] newTexts) {
- if (findTexts.length != newTexts.length) return;
- try {
- View rootView = getRootView(activity);
- if (rootView instanceof ViewGroup) {
- for (int i = 0; i < findTexts.length; i++) {
- traverseAndModifyAllText((ViewGroup) rootView, findTexts[i], newTexts[i]);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- // 递归遍历并修改所有文本
-
- private static boolean traverseAndModifyAllText(ViewGroup parent, String findText, String newText) {
- // 遍历父容器的所有直接子View
- for (int i = 0; i < parent.getChildCount(); i++) {
- View child = parent.getChildAt(i);
- // 处理所有可能的文本控件
- if (tryModifyViewText(child, findText, newText)) {
- return true;
- }
- // 如果这个子View本身也是容器,递归遍历它的子View,递归处理子View
- if (child instanceof ViewGroup) {
- boolean found = traverseAndModifyAllText((ViewGroup) child, findText, newText);
- if (found) return true;
- }
- }
- return false;
- }
-
- // 修改View的文本
-
- private static boolean tryModifyViewText(View view, String findText, String newText) {
- try {
- // TextView及其子类 Button、EditText、CheckBox等
- if (view instanceof TextView) {
- TextView textView = (TextView) view;
- // 检查显示文本
- CharSequence text = textView.getText();
- if (text != null && findText.equals(text.toString())) {
- textView.setText(newText);
- return true;
- }
- // 检查提示文本EditText等
- CharSequence hint = textView.getHint();
- if (hint != null && findText.equals(hint.toString())) {
- textView.setHint(newText);
- return true;
- }
- }
- // Toolbar ActionBar
- if (view instanceof android.widget.Toolbar) {
- android.widget.Toolbar toolbar = (android.widget.Toolbar) view;
- CharSequence title = toolbar.getTitle();
- if (title != null && findText.equals(title.toString())) {
- toolbar.setTitle(newText);
- return true;
- }
- CharSequence subtitle = toolbar.getSubtitle();
- if (subtitle != null && findText.equals(subtitle.toString())) {
- toolbar.setSubtitle(newText);
- return true;
- }
- }
- // SearchView
- if (view instanceof SearchView) {
- SearchView searchView = (SearchView) view;
- // 通过反射获取查询提示
- try {
- Method getQueryHintMethod = SearchView.class.getMethod("getQueryHint");
- CharSequence queryHint = (CharSequence) getQueryHintMethod.invoke(searchView);
- if (queryHint != null && findText.equals(queryHint.toString())) {
- searchView.setQueryHint(newText);
- return true;
- }
- } catch (Exception e) {
- // 忽略反射错误
- }
- }
- // ProgressBar
- if (view instanceof ProgressBar) {
- CharSequence contentDesc = view.getContentDescription();
- if (contentDesc != null && findText.equals(contentDesc.toString())) {
- view.setContentDescription(newText);
- return true;
- }
- }
- // ImageView
- if (view instanceof ImageView) {
- CharSequence contentDesc = view.getContentDescription();
- if (contentDesc != null && findText.equals(contentDesc.toString())) {
- view.setContentDescription(newText);
- return true;
- }
- }
- // 任何View的内容描述
- CharSequence contentDesc = view.getContentDescription();
- if (contentDesc != null && findText.equals(contentDesc.toString())) {
- view.setContentDescription(newText);
- return true;
- }
- // 通过反射尝试获取其他文本属性
- if (tryModifyByReflection(view, findText, newText)) {
- return true;
- }
- } catch (Exception e) {
- // 忽略单个View的修改错误,继续处理其他View
- }
- return false;
- }
-
- // 通过反射修改其他可能的文本属性
-
- private static boolean tryModifyByReflection(View view, String findText, String newText) {
- try {
- // 获取View的所有方法
- Method[] methods = view.getClass().getMethods();
- for (Method method : methods) {
- // 查找可能的文本获取方法
- if (method.getName().startsWith("get") &&
- method.getParameterTypes().length == 0 &&
- CharSequence.class.isAssignableFrom(method.getReturnType())) {
- try {
- CharSequence text = (CharSequence) method.invoke(view);
- if (text != null && findText.equals(text.toString())) {
- // 查找对应的setter方法
- String setterName = "set" + method.getName().substring(3);
- try {
- Method setter = view.getClass().getMethod(setterName, CharSequence.class);
- setter.invoke(view, newText);
- return true;
- } catch (Exception e) {
- // 尝试其他类型的setter
- try {
- Method setter = view.getClass().getMethod(setterName, String.class);
- setter.invoke(view, newText);
- return true;
- } catch (Exception e2) {
- // 继续尝试其他方法
- }
- }
- }
- } catch (Exception e) {
- // 继续尝试其他方法
- }
- }
- }
- } catch (Exception e) {
- // 忽略反射错误
- }
- return false;
- }
-
- // 获取所有包含指定文本的View
-
- public static List<View> findAllViewsWithText(Object activity, String findText) {
- List<View> result = new ArrayList<>();
- try {
- View rootView = getRootView(activity);
- if (rootView instanceof ViewGroup) {
- findAllViewsWithText((ViewGroup) rootView, findText, result);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
-
- //递归查找包含指定文本的View
- private static void findAllViewsWithText(ViewGroup parent, String findText, List<View> result) {
- for (int i = 0; i < parent.getChildCount(); i++) {
- View child = parent.getChildAt(i);
- if (containsText(child, findText)) {
- result.add(child);
- }
- if (child instanceof ViewGroup) {
- findAllViewsWithText((ViewGroup) child, findText, result);
- }
- }
- }
-
- // 检查View是否包含指定文本
-
- private static boolean containsText(View view, String findText) {
- try {
- // TextView及其子类
- if (view instanceof TextView) {
- TextView textView = (TextView) view;
- if (findText.equals(textView.getText().toString()) ||
- findText.equals(textView.getHint().toString())) {
- return true;
- }
- }
- // Toolbar
- if (view instanceof android.widget.Toolbar) {
- android.widget.Toolbar toolbar = (android.widget.Toolbar) view;
- if (findText.equals(toolbar.getTitle().toString()) ||
- findText.equals(toolbar.getSubtitle().toString())) {
- return true;
- }
- }
- // 内容描述
- CharSequence contentDesc = view.getContentDescription();
- if (contentDesc != null && findText.equals(contentDesc.toString())) {
- return true;
- }
- } catch (Exception e) {
- // 忽略错误
- }
- return false;
- }
- // 获取Activity的根视图
- private static View getRootView(Object activity) throws Exception {
- Object window = XposedHelpers.callMethod(activity, "getWindow");
- return (View) XposedHelpers.callMethod(window, "getDecorView");
- }
- }
- }
复制代码 |
|