简体   繁体   English

如何在运行时更改R.String…值

[英]How to change R.String… values in runTime

I have a static values 我有一个静态值

  <integer-array name="accordion_visibility">
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>0</item>
</integer-array>

I am trying to change this values in run time. 我试图在运行时更改此值。 but unable to do it 但无法做到

You can't and it's not the good approach trying to do it. 您不能,而且这不是尝试执行此操作的好方法。 You always can retrieve them, use them and modify them if it's necessary and save them if you need save the state. 您始终可以检索它们,使用它们并在必要时对其进行修改,并在需要保存状态时进行保存。 You can use SharedPreferences to save data between executions easily. 您可以使用SharedPreferences轻松保存SharedPreferences执行之间的数据。

You can't change strings.xml dynamically since it's a compiled resource. 由于它是已编译的资源,因此无法动态更改strings.xml。 But you can use the sharedPreference to change the values dynamically. 但是您可以使用sharedPreference动态更改值。

You cannot modify strings.xml in runTime. 您无法在运行时中修改strings.xml。 If you want to store some strings on the phone's storage use SharedPreferences . 如果要将一些字符串存储在手机的存储中,请使用SharedPreferences This is how I usually use it: 这是我通常使用的方式:

public class Preferences {

    // Constant key values
    public static final String SERVER_IP  = "Server"; 
    // {....}
    public static final String SOUND = "Notif sound";


    // Required classes for SharedPreferences
    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;

    public Preferences(Context context) {
        this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
        this.editor = this.sharedPreferences.edit();
    }

    /**
     * Set a new value in the shared preferences.
     * <p>
     * You can get the value by the get function.
     * @param key   key of the value, one of the constant strings of {@link Preferences}'s.
     * @param value the value, that should be stored.
     *
     * @see     #get(String, String)
     *
     */
    public void set(String key, String value) {
        this.editor.putString(key, value);
        this.editor.commit();
    }

    /**
     * Get the value of a previously set data if exact or return the default value if not.
     * @param key           key of the value, one of the constant strings of {@link Preferences}'s.
     * @param defaultValue  the default value, return this if the key is not in the database.
     * @return              the value belongs to the key or the default value.
     *
     * @see     #set(String, String)
     */
    public String get(String key, String defaultValue) {
        return this.sharedPreferences.getString(key, defaultValue);
    }

    /**
     * Removes a key value pair from the database.
     * @param key   The key of the pair which should be removed.
     */
    public void clear(String key) {
        this.editor.remove(key);
        this.editor.commit();
    }

    /**
     * Delete all key value pairs from the database.
     */
    public void clear() {
        Log.d(TAG,"SharedPreferences cleared");
        this.editor.clear();
        this.editor.commit();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM