SharedPreferences如何保存文本视图和背景色

SharedPreferences how to save text view and bg color

大家好,首先我整天都在为此寻找答案,但我一无所获,我试图保存一个简单的东西,我需要保存背景颜色和用户按下的 "points" a button 我会解释用户需要按 button 来更改 background color 并在 button 之后将 textview 增加 +1.

在我关闭应用程序后 return 我希望应用程序显示用户选择的最后一个背景颜色以及点击 +1 button 的时间:

这里是代码: 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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.hanansanag.colorbeck.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="home work :)"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="change bg :"
        android:textStyle="bold"
        android:textSize="15sp"
        android:id="@+id/Tv2"
        android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/cred"
            android:text="red"
            android:background="@drawable/redbutton"
            android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:text="green"
            android:id="@+id/cgreen"
            android:layout_marginLeft="10dp"
            android:background="@drawable/greenbtm"
            android:textAllCaps="false"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:background="@drawable/blue222"
            android:id="@+id/cblue"
            android:layout_marginLeft="10dp"
            android:text="blue"
            android:textAllCaps="false"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="70dp"
            android:layout_height="70dp"

            android:background="@drawable/easyyyyy"
            android:id="@+id/btnadd"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/tv"
            android:textSize="20sp"
            android:layout_marginLeft="70dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="70dp"
            android:id="@+id/save"
            android:layout_height="70dp"
            android:layout_marginLeft="110dp"
            android:background="@drawable/save"/>
    </LinearLayout>


</LinearLayout>

这里是 java 代码:

    package com.example.hanansanag.colorbeck;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnred,btngreen,btnblue,btnpluse,btnsave;
    private LinearLayout mlinearLayout;
    private TextView tv;
    int point = 0;
    public SharedPreferences sp;
    private int progress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp= getSharedPreferences("save",0);
        String textValue = sp.getString("textvalue","");

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);

        btnred = (Button) findViewById(R.id.cred);
        btnblue = (Button) findViewById(R.id.cblue);
        btngreen = (Button) findViewById(R.id.cgreen);
        btnpluse = (Button)findViewById(R.id.btnadd);
        btnsave = (Button)findViewById(R.id.save);
        tv=(TextView)findViewById(R.id.tv) ;
        btnred.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        btnpluse.setOnClickListener(this);
        btnblue.setOnClickListener(this);
        btngreen.setOnClickListener(this);


        }



    @Override
    public void onClick(View v) {
        if (btnred == v){
            mlinearLayout.setBackgroundColor(Color.RED);

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);

        }
        else if (btnpluse== v){
            point++;
            tv.setText("" + point);

        }
        else if (btnsave == v){
            Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("textValue",tv.getText().toString());
            editor.commit();
        }
    }
}

就像你看到的那样,我在 TextviewSharedPreferences 尝试,但在我关闭后 return 我什么也没看到我知道我做错了什么,但我不知道是什么。 为大家提供帮助

将 btnsave 中的 SharedPreference 位更改为:

SharedPreferences.Editor editor = getSharedPreferences(save, MODE_PRIVATE).edit();
 editor.putString("textValue",tv.getText().toString());
 editor.commit();

并且在 OnCreate 中将 SharedPreference 位更改为:

SharedPreferences prefs = getSharedPreferences(save, MODE_PRIVATE); 
String textValue= prefs.getString("textValue", "0");

并在将 tv 声明为 TextView 后添加此代码。

tv.setText(textValue)

在每个用于更改背景颜色的按钮中,放入此代码并将我的变量 'COLOUR' 更改为 'BLUE'、'RED' 等

SharedPreferences.Editor editor = getSharedPreferences(saveColour, MODE_PRIVATE).edit();
editor.putString("colourValue",COLOUR);
editor.commit();

然后在创建时调用:

SharedPreferences prefs = getSharedPreferences(saveColour, MODE_PRIVATE); 
String colourValue = prefs.getString("colourValue", WHITE);

并且在声明 mLinearLayout 之后:

mlinearLayout.setBackgroundColor(Color.colourValue);