Android 如何更改R.String。。。。运行时的值

Android 如何更改R.String。。。。运行时的值,android,Android,我有一个静态值 <integer-array name="accordion_visibility"> <item>1</item> <item>0</item> <item>0</item> <item>1</item> <item>0</item> <item>0</item>

我有一个静态值

  <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>

1.
0
0
1.
0
0
0

我试图在运行时更改此值。但无法执行此操作

您无法动态更改strings.xml,因为它是一个已编译的资源。但您可以使用SharedReference动态更改值

无法动态更改strings.xml,因为它是已编译的资源。但您可以使用SharedReference动态更改值

你做不到,这不是一个好办法。
您始终可以检索它们、使用它们并在必要时修改它们,以及在需要时保存它们以保存状态。您可以使用
SharedReferences
轻松地在执行之间保存数据。

您不能这样做,尝试这样做不是一个好方法。
您始终可以检索它们、使用它们并在必要时修改它们,以及在需要时保存它们以保存状态。您可以使用
SharedReferences
在执行之间轻松保存数据。

您不能在运行时修改strings.xml。如果您想在手机的存储器中存储一些字符串,请使用。我通常是这样使用它的:

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();
    }
}
公共类首选项{
//常量键值
公共静态最终字符串服务器\u IP=“SERVER”;
// {....}
公共静态最终字符串SOUND=“Notif SOUND”;
//SharedReference所需的类
私人最终股份参考;股份参考;
私人最终共享参考。编辑;
公共首选项(上下文){
this.sharedPreferences=context.getSharedPreferences(MY_PREF,0);
this.editor=this.SharedReferences.edit();
}
/**
*在共享首选项中设置新值。
*
*您可以通过get函数获取值。
*@param key值的键,{@link Preferences}的常量字符串之一。
*@param value应存储的值。
*
*@see#get(String,String)
*
*/
公共无效集(字符串键、字符串值){
this.editor.putString(键,值);
this.editor.commit();
}
/**
*如果精确,则获取先前设置的数据的值;如果不精确,则返回默认值。
*@param key值的键,{@link Preferences}的常量字符串之一。
*@param defaultValue默认值,如果密钥不在数据库中,则返回此值。
*@return该值属于键或默认值。
*
*@see#set(String,String)
*/
公共字符串获取(字符串键、字符串默认值){
返回此.SharedReferences.getString(键,默认值);
}
/**
*从数据库中删除键值对。
*@param key应删除的密钥对的密钥。
*/
公共无效清除(字符串键){
此.editor.remove(键);
this.editor.commit();
}
/**
*从数据库中删除所有键值对。
*/
公共空间清除(){
Log.d(标记“SharedReferences cleared”);
this.editor.clear();
this.editor.commit();
}
}

您不能在运行时修改strings.xml。如果您想在手机的存储器中存储一些字符串,请使用。我通常是这样使用它的:

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();
    }
}
公共类首选项{
//常量键值
公共静态最终字符串服务器\u IP=“SERVER”;
// {....}
公共静态最终字符串SOUND=“Notif SOUND”;
//SharedReference所需的类
私人最终股份参考;股份参考;
私人最终共享参考。编辑;
公共首选项(上下文){
this.sharedPreferences=context.getSharedPreferences(MY_PREF,0);
this.editor=this.SharedReferences.edit();
}
/**
*在共享首选项中设置新值。
*
*您可以通过get函数获取值。
*@param key值的键,{@link Preferences}的常量字符串之一。
*@param value应存储的值。
*
*@see#get(String,String)
*
*/
公共无效集(字符串键、字符串值){
this.editor.putString(键,值);
this.editor.commit();
}
/**
*如果精确,则获取先前设置的数据的值;如果不精确,则返回默认值。
*@param key值的键,{@link Preferences}的常量字符串之一。
*@param defaultValue默认值,如果密钥不在数据库中,则返回此值。
*@return该值属于键或默认值。
*
*@see#set(String,String)
*/
公共字符串获取(字符串键、字符串默认值){
返回此.SharedReferences.getString(键,默认值);
}
/**
*从数据库中删除键值对。
*@param key应删除的密钥对的密钥。
*/
公共无效清除(字符串键){
此.editor.remove(键);
this.editor.commit();
}
/**
*从数据库中删除所有键值对。
*/
公共空间清除(){
Log.d(标记“SharedReferences cleared”);
this.editor.clear();
this.editor.commit();
}
}

correct您无法更改它们,它们是只读的,那么还有其他方法吗?correct您无法更改它们,它们是只读的,那么还有其他方法吗?