android 颜色共享首选项不起作用

android sharedpreferences for color not working

// 我的程序运行良好,没有错误,更改工具栏颜色时颜色也可以正常工作。但 sharedpreferences 不适用于 color ,它无法保存更改后的颜色。在关闭应用程序后再次重新启动应用程序时更改工具栏的颜色后。它以默认颜色开头,我无法使用共享首选项保存。//

public class MainActivity extends AppCompatActivity {
Toolbar mtoolbar;
Button mredcolour;
Button mgreencolour;
Button myellowcolour;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mtoolbar =(Toolbar) findViewById(R.id.toolbar2);
    mredcolour=(Button) findViewById(R.id.button);
    mgreencolour=(Button) findViewById(R.id.button2);
    myellowcolour=(Button) findViewById(R.id.button3);

    if(getcolour()!=getResources().getColor(R.color.colorPrimary)){
        mtoolbar.setBackgroundColor(getcolour());

            getWindow().setStatusBarColor(getcolour() );

    }
    mredcolour.setOnClickListener(new View.OnClickListener(){


        @Override
        public void onClick(View v) {
            mtoolbar.setBackgroundColor(getResources().getColor(R.color.colorred));

               getWindow().setStatusBarColor(getResources().getColor(R.color.colorred));

           storecoloru(getResources().getColor(R.color.colorred));
        }
    });
    mgreencolour.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            mtoolbar.setBackgroundColor(getResources().getColor(R.color.colorgreen));

                getWindow().setStatusBarColor(getResources().getColor(R.color.colorgreen));

            storecoloru(getResources().getColor(R.color.colorgreen));
        }
    });
    myellowcolour.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mtoolbar.setBackgroundColor(getResources().getColor(R.color.coloryellow));

                getWindow().setStatusBarColor(getResources().getColor(R.color.coloryellow));

            storecoloru(getResources().getColor(R.color.coloryellow));
        }
    });
}
private void storecoloru(int color){
    SharedPreferences msharedpreferences =getSharedPreferences("toolbarcolour",MODE_PRIVATE);
    SharedPreferences.Editor meditor=msharedpreferences.edit();
    meditor.putInt("color",color );
    meditor.apply();

}
private  int getcolour(){
    SharedPreferences msharedpreferences= getSharedPreferences("toolbarcolour",MODE_PRIVATE );
    int selectedcolour=msharedpreferences.getInt("colour",getResources().getColor(R.color.colorPrimary) );
    return selectedcolour;
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="0dp"
    tools:ignore="MissingConstraints" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="26dp"
    tools:layout_editor_absoluteY="287dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="362dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="464dp" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorred">#ff0000</color>
<color name="colorgreen">#008000</color>
<color name="coloryellow">#ffff00</color>
</resources>

您正在使用不同的密钥获取颜色

int selectedcolour=msharedpreferences.getInt("colour",getResources().getColor(R.color.colorPrimary) );

您在保存到共享首选项时使用 color,在检索时使用 colour。如果这是在 Whosebug 中键入代码时的错误,那么我们将需要更多信息来解决您的问题。