V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
chenyu8674
V2EX  ›  Android

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

  •  
  •   chenyu8674 · Sep 15, 2020 · 12777 views
    This topic created in 2055 days ago, the information mentioned may be changed or developed.
    接口需以大写字母入参,APP 中对 EditText 使用 ReplacementTransformationMethod 进行了强制大写转换,提交时使用 getText()获取内容,但项目上线后发现极少数请求的入参依然是小写字母

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

    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));
    Supplement 2  ·  Sep 16, 2020
    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 replies    2020-09-22 10:11:19 +08:00
    Lin0936
        1
    Lin0936  
       Sep 15, 2020
    复制粘贴?
    300
        2
    300  
       Sep 15, 2020 via Android
    直接扒接口了?
    chenyu8674
        3
    chenyu8674  
    OP
       Sep 15, 2020
    @Lin0936 #1 自己测试时复制粘贴依然是大写

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