V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenyu8674
V2EX  ›  Android

请问诸位 Android 开发大佬, EditText 的 ReplacementTransformationMethod 有什么办法绕过去

  •  
  •   chenyu8674 · 2020-09-15 13:01:55 +08:00 · 4242 次点击
    这是一个创建于 1311 天前的主题,其中的信息可能已经有所发展或是发生改变。
    接口需以大写字母入参,APP 中对 EditText 使用 ReplacementTransformationMethod 进行了强制大写转换,提交时使用 getText()获取内容,但项目上线后发现极少数请求的入参依然是小写字母

    虽然后台有对应并不影响业务,也在 APP 中提交时加了 toUpperCase 处理,但没想明白 ReplacementTransformationMethod 是怎么被绕过的,在几十台测试机上试过各种骚操作也再现不出来
    第 1 条附言  ·  2020-09-15 14:40:16 +08:00
    附代码

    public static class AllCapTransformationMethod extends ReplacementTransformationMethod {
    private char[] lower = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private char[] upper = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    private boolean allUpper;

    public AllCapTransformationMethod(boolean needUpper) {
    this.allUpper = needUpper;
    }

    protected char[] getOriginal() {
    if (allUpper) {
    return lower;
    } else {
    return upper;
    }
    }

    protected char[] getReplacement() {
    if (allUpper) {
    return upper;
    } else {
    return lower;
    }
    }
    }

    使用(项目中不含 needUpper 为 false 的调用):
    editText.setTransformationMethod(new AllCapTransformationMethod(true));
    第 2 条附言  ·  2020-09-16 11:15:26 +08:00
    http://web.mit.edu/afs.new/sipb/project/android/docs/reference/android/widget/TextView.html#setTransformationMethod(android.text.method.TransformationMethod)

    public final void setTransformationMethod (TransformationMethod method)
    Added in API level 1
    Sets the transformation that is applied to the text that this TextView is displaying.

    setTransformationMethod 只影响显示内容
    12 条回复    2020-09-22 10:11:19 +08:00
    Lin0936
        1
    Lin0936  
       2020-09-15 13:11:11 +08:00
    复制粘贴?
    winterbells
        2
    winterbells  
       2020-09-15 13:15:01 +08:00 via Android
    直接扒接口了?
    chenyu8674
        3
    chenyu8674  
    OP
       2020-09-15 13:54:13 +08:00
    @Lin0936 #1 自己测试时复制粘贴依然是大写

    @winterbells #2 给合作单位的人员使用的,应该不会有人扒接口
    cczhrd
        4
    cczhrd  
       2020-09-15 14:15:19 +08:00
    提交时加了 toUpperCase 处理 还能发现入参依然是小写字母?
    kop1989
        5
    kop1989  
       2020-09-15 14:25:49 +08:00
    稍微看了下源码,感觉逻辑上不太能导致无效(也有可能是我没理解其中奥妙😂)。
    会不会是部分手机型号 /系统版本的问题?或者是出问题的输入值有没有什么规律?再或者是 getReplacement 这个实现本身程序的问题?
    chenyu8674
        6
    chenyu8674  
    OP
       2020-09-15 14:35:50 +08:00
    @cczhrd #4 toUpperCase 是发现问题后加上的,不然也不会定位到 ReplacementTransformationMethod 了
    Lin0936
        7
    Lin0936  
       2020-09-15 14:50:50 +08:00
    貌似看不出来什么问题,tracker 看一下是不是跟系统版本 /手机型号有关吧。
    nznd
        8
    nznd  
       2020-09-15 15:18:50 +08:00
    有的系统级复制粘贴是直接改属性的 value,比如可以在计算器随意粘贴中文,这个看手机系统的
    chenyu8674
        9
    chenyu8674  
    OP
       2020-09-15 15:23:18 +08:00
    @Lin0936 #7
    出问题的手机是荣耀 9X
    我们有同型号测试机,但两边都再现不出该问题了……
    chenyu8674
        10
    chenyu8674  
    OP
       2020-09-15 16:28:47 +08:00
    @nznd #8
    还真没遇到过,请问哪些系统会有这种现象,我看看能不能搞台机器测试下
    Michelangelono
        11
    Michelangelono  
       2020-09-15 16:31:50 +08:00 via Android
    接口请求的时候上传手机型号
    qihehulian
        12
    qihehulian  
       2020-09-22 10:11:19 +08:00 via Android
    Xposed.findAndHookMethod(EditText.class, "getText", new MyMethodHook());,完事。MyMethodHook:afterMethodHooked 转换大写即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:00 · PVG 16:00 · LAX 01:00 · JFK 04:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.