简体   繁体   English

boolean和if条件的Android Studio SharedPreferences

[英]Android Studio SharedPreferences of boolean and if conditions

Task: - When a save button is clicked: Save a checkbox's boolean value to SharedPreferences. 任务:-单击保存按钮时:将复选框的布尔值保存到SharedPreferences。 - On App boot check if the SharedPreferences value is true, if so check the checkbox. -在应用启动时,检查SharedPreferences值是否为true,如果是,则选中复选框。

Not sure in which part of the above i'm wrong, so i present you the whole thing. 不知道上面哪一部分是我错的,所以我向您介绍整个过程。 (Searched a lot the web and trying to solve this for days, excuse y newbie-city) (请在新手城中搜索很多网页,然后尝试解决几天的问题)

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {
    boolean ever_saved_settings=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String if_settings= String.valueOf(ever_saved_settings);
    if(if_settings.equals("true")) {
        SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);
        String PU2 = Saved_Settings.getString("PU", null);
        if (PU2=="true")
            chk_box_PU.setChecked(true);
            else
            chk_box_PU.setChecked(false);

    }

public void save_settings(View v) {
    ever_saved_settings=true;
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);
    String PU_value = (String) chk_box_PU.getText();
    SharedPreferences Saved_Settings = this.getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();
    prefsEditor.putString("PU", PU_value);
    prefsEditor.commit();
}
}

EDIT 编辑

Here arethe /xml's just in case the problem is not in the activity. 这是/ xml的情况,以防问题不在活动中。 Rare but possible for a newbie :} 很少见,但对于新手来说是可能的:}

toolbar_save_button.xml toolbar_save_button.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal">


<Button
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:text="@string/save_button"
    android:textSize="24dp"
    android:onClick="save_settings"/>

which is included in: home.xml 其中包括:home.xml

<merge
xmlns:android="http://schemas.android.com/apk/res/android">
[...]
<include layout="@layout/toolbar_save_button"/>
</merge>

whish is included in: activity_main.xml 愿望包含在:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/back_0"
tools:context=".MainActivity">
[...]
<include layout="@layout/home"/>
[...]
</LinearLayout>

The button looks just fine running the .apk in an android cell phone, it "blibs" when you click it. 在Android手机中运行.apk时,该按钮看起来还不错,单击该按钮时它会“嗡嗡作响”。

Try this: 尝试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    SharedPreferences Saved_Settings = getPreferences("Saved_Settings", MODE_PRIVATE);
    boolean PU2 = Saved_Settings.getBoolean("PU", false);

    // no need to check with if conditions. just set the button with the value(PU2)
    chk_box_PU.setChecked(PU2);

}

public void save_settings(View v) {
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);
    SharedPreferences Saved_Settings = this.getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();
    prefsEditor.putBoolean("PU", chk_box_PU.isChecked());
    prefsEditor.commit();
}

1. You don't need this variable: 1.您不需要此变量:

boolean ever_saved_settings=false;

This variable is set to false every time you launch your application. 每次启动应用程序时,此变量都设置为false。 Also you will not get any error if settings don't exist, so the variable is useless. 如果设置不存在,也不会出现任何错误,因此该变量无用。


2. Your line 2.你的线

if (PU2=="true")

will always return false. 将始终返回false。 In this line you are comparing pointers, not values. 在这一行中,您正在比较指针,而不是值。

Always use it in this way: 始终以这种方式使用它:

if (PU2.equals("true"))


3. Why do you store boolean values in a String, and compare them as Strings? 3.为什么将布尔值存储在字符串中,然后将它们作为字符串进行比较?

You can compare boolean variables, for example: 您可以比较布尔变量,例如:

boolean variable = true;
if (variable) {
    // do your stuff
}


4. here is how you should implement it: 4.这是您应如何实施:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences Saved_Settings = getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    chk_box_PU.setChecked(Saved_Settings.getBoolean("PU", false));
}

public void save_settings(View v) {
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);

    SharedPreferences Saved_Settings = getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();

    prefsEditor.putBoolean("PU", chk_box_PU.isChecked());

    prefsEditor.apply();
}}

I think that you are missing the preference name: 我认为您缺少首选项名称:

   SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);

Change to: 改成:

   SharedPreferences Saved_Settings = getPreferences("Saved_Settings", MODE_PRIVATE);

in your onCreate() change this 在您的onCreate()中更改此

SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);

to

SharedPreferences Saved_Settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

and this if (PU2=="true") to if(PU2.equals("true")) 这是if (PU2=="true")if(PU2.equals("true"))

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

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