如何将 variable/parameter 传递给 android 查看方法

How to pass variable/parameter to android view method

我是 Whosebug 的新手 JAVA。 忽略 catch 这工作正常。

public int GetHeight (AndroidViewComponent Component) {
        try {
            return Component.getView().getHeight();
        }
        catch (Exception e) {
            return -0000;
        }
    }

我遇到构建错误。

public int GetMethod (AndroidViewComponent Component , string get) {
        try {
            return Component.getView().get();
        }
        catch (Exception e) {
            return -0000;
        }
    }

我想减少代码。 如果,否则如果,......否则 所以我想把get参数直接传给方法

Component.getView().get();

有什么办法吗?

您不能在函数的参数中传递要访问的方法名,因为编译代码时,所有方法名都会重命名为随机变量,以防止黑客攻击。所以没有动态调用变量名。

除非您从字符串参数中执行 if/else 并以这种方式调用方法。示例:-

public int GetXonScreen (AndroidViewComponent Component, String nameOfMethod) {
        try {
            int[] location = new int[2];
            if (nameOfMethod == "height") {
              return Component.getView().getHeight();
            } else if (nameOfMethod == "width") {
              return Component.getView().getWidth();
            } else {return 0;}
        }
        catch (Exception e) {
            return -0000;
        }
    }