gpt4 book ai didi

java - 从函数/方法设置 "android:text"值

转载 作者:行者123 更新时间:2023-12-02 11:04:33 26 4
gpt4 key购买 nike

理论上实现存储在远程数据库中的多文化。

在 Android 中,我们有以下内容:

 <TextView
android:id="@+id/loginlabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:text="@string/login" />

并且 text 使用文件 string.xml 中的属性 login 的值。

现在我的问题:在属性 android:text 的 Activity xml 中,有什么方法可以让我调用函数/方法(甚至可以来自静态类)吗?一个有一些业务逻辑但仍然返回字符串的地方。

我知道我可以在我的 Activity java 文件中轻松使用以下类型的代码:

TextView loginlabel = (TextView) findViewById(R.id.loginlabel);
loginlabel.setText( GetMyText(R.id.loginlabel) );

但是我试图避免将其写入“一百万次”,因为我的 strings.xml 上有多个 Activity 和数百个值。

对于附加信息,我这样做的目的如下:

  • 在应用程序启动时,我读取所有 string.xml 并连接到我的远程服务器(具有数据库)并验证所有字符串是否都插入到该数据库中。
  • 插入缺少的属性,并使用 string.xml 中该属性的默认值。
  • 现在,在另一个远程系统中,客户端可以动态创建该属性的新文化和翻译。
  • 回到 Android,我加载当前可用的文化列表,并从数据库加载值(翻译)。唯一缺少的就是将它们“注入(inject)”到我使用“字符串”值的地方。

最佳答案

这在过去对我有用。

public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);

setText(yourMethodWithBusinessLogic());
}
}

不要忘记用这个自定义 TextView 替换您的普通 TextView 。为了方便起见,请按 CTRL+SHIFT+R。

希望这有帮助。

关于java - 从函数/方法设置 "android:text"值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51078110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com