【问题标题】:How to maintain String constant across java and xml file?如何跨 java 和 xml 文件维护字符串常量?
【发布时间】:2023-03-18 19:40:01
【问题描述】:

Android 框架中的许多组件都需要在 java 和 xml 文件中使用字符串常量。例如,要实现具有自定义帐户类型的自定义帐户身份验证器,我们将需要在account-authenticator xml 文件和 java 代码中使用帐户类型字符串“my_custom_account_type”。

身份验证器.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <account-authenticator
        ...
        android:accountType="my_custom_account_type"
        ...
        />

并通过执行以下操作在我们的应用中添加一个帐户:

    Account myAccount = new Account("John", "my_custom_account_type");
    AccountManager.get(mContext).addAccountExplicitly(myAccount, null, null);

问题:这通常是如何完成的?人们是否只是在心里记下自己要始终记住在多个位置更新这些字符串?是否有维护这些字符串的标准方法?

编辑: 我考虑过使用在字符串资源中定义字符串常量并调用 Context.getString() 以在 java 代码中检索它。然而,这将要求我们有一个Context 对象。我想知道是否有更通用的方法来解决这个问题。

【问题讨论】:

    标签: android


    【解决方案1】:

    我考虑过在字符串资源中定义字符串常量并调用Context.getString() 在java 代码中检索它。但是,这将要求我们有一个 Context 对象。我想知道是否有更通用的方法来解决这个问题。

    如果保留上下文对象很麻烦,您可以创建一个包含静态字符串变量的类并在自定义应用程序类中初始化它。

    类似的东西

    public class StringRegister{
        static String custom_account_type;
    
        public static void initialize(Context context){
            this.custom_account_type = context.getResources().getString(R.string.custom_account_type);
        }
    
        public static String get_custom_account_type(){
            if(custom_account_type != null)
                return custom_account_type;
            return null;
        }
    }
    
    public class CustomApp extends Application{
    
        @Override
        public void onCreate(){
            super.onCreate();
            StringRegister.initialize(this);
        }
    }
    

    将 CustomApp 添加到您的清单中

    <application
        android:name=".CustomApp"
    

    您可以通过调用 StringRegister.get_custom_account_type()

    访问您的 String 变量

    编辑:我应该补充一点,虽然这可行,但它并不漂亮。保留上下文对象会更干净。

    【讨论】:

    • 考虑一下:我有一个内容提供者和一个 SyncAdapter。我定义了字符串资源&lt;string name="my_authority"&gt;my.authority&lt;/string&gt;。我的同步适配器 xml 文件中有:android:contentAuthority="@string/my_authority"。我的内容提供程序类中有static final String AUTHORITY = MyApp.getContext().getString(R.string.my_authority),所以我可以使用 UriMatcher。 MyAppApplication 的子类,MyApp.getContext() 是一个静态方法,它将返回一个 MyApp 的实例,我存储在 MyApponCreate() 期间。我现在允许其他应用访问我的内容提供商。
    • 这种需要上下文的方法是否仍然有效?我假设如果 MyApp 未实例化,当其他应用程序尝试访问我的内容提供程序时,我将收到 NullPointerException。
    • 让我重新表述您的评论,以确保我理解正确。您想将 appcontext 存储在静态变量中吗?可能,但可能导致一些巨大的内存泄漏。老实说,在进行这么长时间之前,我宁愿将上下文作为参数传递。如果传递上下文是一个问题,您的代码可能需要重新设计。编辑:我也不确定,“其他应用程序”是什么意思。应用程序上下文只能由应用程序本身访问。 App A 无法访问 App B 的上下文
    • 我想在 java 代码和 xml 中保持字符串常量。您的答案和这里的许多其他答案是将字符串保存在资源文件中并使用 Context 在 java 代码中检索字符串的变体。我的评论描述了一个外部应用程序访问我的内容提供程序,试图找到一个可能无法(不太确定)有一个正确的 Context 实例来调用 getString() 的场景。
    • 哦,好吧,所以您本质上想要的是从不同的应用程序访问相同的字符串。我认为 Android Studio 不可能(我可能错了),但是您可以将字符串资源提取到一个单独的文件中,在每个需要它的项目中保留相同的文件,并在您的工作站上构建一个脚本来同步这些所有项目的文件
    【解决方案2】:

    转到res/values/string.xml 文件。您可以在此处定义要在整个应用程序中使用的任何字符串常量。

    例如

    <string name="Sample">This is sample string.</string>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
    

    现在,您只需在需要时引用字符串。

    对于 XML 文件

    android:text="@string/Sample"
    

    对于 JAVA 代码

    getApplicationContext().getResources().getString(R.string.Sample);
    

    getApplicationContext().getResources().getText(R.string.Sample);
    

    对于数组,

    getApplicationContext().getResources().getStringArray(R.string.planets_array)
    

    您可以使用getString(int)getText(int) 来检索字符串。 getText(int) 将保留应用于字符串的任何富文本样式。

    参考文档:here

    【讨论】:

      【解决方案3】:

      您可以使用getString(R.string.your_string); 访问在<resources> <string name="your_string">Required String Value</string> </resources> 中声明的字符串

      【讨论】:

        【解决方案4】:

        在 Strings.xml 文件中定义字符串

        您可以使用@string/string_name 在xml 中使用该字符串。 在 Java 中你可以使用

        getString(R.string.string_name);
        

        【讨论】:

        • 但这需要一个上下文来调用 getString()。
        • 如果你在片段中然后调用 getActivity().getString()
        • 如果您在适配器或任何其他类中,则在该类/适配器的构造函数中传递 Activity 的上下文。 example class MyClass{ public MyClass(Context context){ //将此上下文保存到全局变量。 } }
        【解决方案5】:

        我会定义一个字符串资源并在 xml 和代码中引用它:

        <resources>
            <string name="custom_account_type">my_custom_account_type</string>
        </resources>
        

        然后:

        <account-authenticator
            ...
            android:accountType="@string/custom_account_type"
            ...
            />
        

        和:

        Account myAccount = new Account("John", mContext.getString(R.string.custom_account_type));
        

        这样你只需要在一个地方更新字符串。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-14
          • 2011-02-03
          • 2014-09-14
          • 2015-09-17
          • 2020-05-01
          • 1970-01-01
          • 2023-03-08
          相关资源
          最近更新 更多