Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android交换机问题_Java_Android - Fatal编程技术网

Java Android交换机问题

Java Android交换机问题,java,android,Java,Android,我目前正在Android应用程序中使用一个开关作为设置类中的一种选项。但是,当我将开关的状态从say false更改为true,然后返回菜单,然后返回设置活动时,开关的状态将返回默认值。是否有一条基本上保存交换机状态的关键线路,这样当用户返回时,状态是相同的?我不认为这个问题需要代码,但如果你认为它有帮助,请让我知道,我会附上它。谢谢 这是我的活动代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an

我目前正在Android应用程序中使用一个开关作为设置类中的一种选项。但是,当我将开关的状态从say false更改为true,然后返回菜单,然后返回设置活动时,开关的状态将返回默认值。是否有一条基本上保存交换机状态的关键线路,这样当用户返回时,状态是相同的?我不认为这个问题需要代码,但如果你认为它有帮助,请让我知道,我会附上它。谢谢

这是我的活动代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.speedytext.Options" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="46dp"
    android:layout_marginTop="48dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Switch" />

<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/switch1"
    android:layout_below="@+id/switch1"
    android:layout_marginTop="66dp"
    android:text="Switch" />

</RelativeLayout>
在onCreate中,使用存储的is checked值调用以将默认值设置为以前存储的值


也可以考虑将设置存储到共享首选项中。静态变量不会在应用程序重新启动时保持不变,通常会使代码更难维护。

共享首选项类

public class AppPreferences {

    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;

    public AppPreferences(Context context, String Preferncename) {
        this.appSharedPrefs = context.getSharedPreferences(Preferncename,
                Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }

    /****
     * 
     * getdata() get the value from the preference
     * 
     * */
    public String getData(String key) {
        return appSharedPrefs.getString(key, "");
    }

    /****
     * 
     * SaveData() save the value to the preference
     * 
     * */
    public void SaveData(String text, String Tag) {
        prefsEditor.putString(Tag, text);
        prefsEditor.commit();

    }


    public void clear()
    {

         prefsEditor.clear();
         prefsEditor.commit();
    }

}
共享首选项在活动中的使用

AppPreferences appPref;

appPref = new AppPreferences(getApplicationContext(), "PREFS");
拯救国家

当开关的状态更改时,将状态true或false保存到共享首选项中的标记

您可以使用此行保存状态

appPref.SaveData("Value", "Tag");
因此,对于选中状态appPref.SaveDatatrue,state,它将是这样的

和appPref.SaveDatafalse,state;对于未检查状态

在活动的oncreate中,从共享首选项中获取值 使用

然后检查结果字符串的true或false
并相应地设置开关

一些代码非常有用!是的,我们编了一些代码。您使用的是常用开关还是首选开关?只有switchPreference可以保存它的状态,而普通的Switch不能。你应该将你的Switch状态保存到arraylist中,然后将它们保存在savedInstanceState或shared Preferences中。是的,我不知道为什么我没有想到这一点……常识。谢谢那么现在的问题是什么呢?我想这是因为我没有添加共享首选项部分。如果返回菜单活动并返回此选项活动,则开关的状态将丢失。让我尝试添加共享首选项部分!谢谢appPref.getDatastate是否应返回布尔值或表示true或false的字符串?字符串将是y的返回类型。我将结果字符串与equals方法进行了比较。哦,很抱歉,我没有看到编辑,因为我没有刷新页面。让我试试看它是否有效!所以你提到最后一部分应该在我的oncreate中。但是AppPreferences appPref呢;appPref=新的AppPreferencesgetApplicationContext,PREFS;和appPref.SaveDataValue,Tag;。在同一个OnCreate中不是也一样吗?在OnCreate方法中调用appPref.getDatastate之前,应该初始化所有内容
appPref.SaveData("Value", "Tag");
appPref.getData("state");
if(appPref.getData("state").equals("true"))
{
//set ur switch button to checked state
}
else if(appPref.getData("state").equals("false"))
{
//set ur switch button to unchecked state
}