Java SharedReferences如何保存文本视图和背景颜色

Java SharedReferences如何保存文本视图和背景颜色,java,android,android-studio,android-sharedpreferences,Java,Android,Android Studio,Android Sharedpreferences,嗨,大家好,第一件事,我整天都在寻找答案,但我什么也没发现我试图保存一件简单的事情,我需要保存背景色和“点”用户按下按钮我将解释用户需要按下改变背景颜色的按钮,然后按下将文本视图增加+1的按钮 在我关闭应用程序并返回后,我希望应用程序将显示用户选择的最后一种背景颜色以及单击+1按钮的时间: 下面是代码: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sch

嗨,大家好,第一件事,我整天都在寻找答案,但我什么也没发现我试图保存一件简单的事情,我需要保存背景色和“点”用户按下
按钮
我将解释用户需要按下改变
背景颜色的
按钮
,然后按下将
文本视图
增加+1的
按钮

在我关闭应用程序并返回后,我希望应用程序将显示用户选择的最后一种背景颜色以及单击+1
按钮的时间:

下面是代码: 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>
    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();
        }
    }
}
就像你看到的,我在
文本视图上尝试了
共享参考
,但在我关闭并返回后,我什么也看不到,我知道我做错了什么,但我不知道是什么。
将btnsave中的SharedReference位更改为:

SharedPreferences.Editor editor = getSharedPreferences(save, MODE_PRIVATE).edit();
 editor.putString("textValue",tv.getText().toString());
 editor.commit();
在OnCreate中,将SharedReference位更改为:

SharedPreferences prefs = getSharedPreferences(save, MODE_PRIVATE); 
String textValue= prefs.getString("textValue", "0");
并在将tv声明为TextView后添加此代码

tv.setText(textValue)
在更改背景颜色的每个按钮中,输入此代码并将我的变量“color”更改为“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.setBackgroundColor(Color.colourValue);

你想在sp中保存什么?@W4R10CK就像我说的那样“在我关闭应用程序并返回我想要的内容后,应用程序将显示用户选择的最后一种bg颜色,以及单击+1按钮的方式”ty代表你的时间,兄弟,但在我关闭应用程序并返回电视节目0之后,在我关闭之前,我在按钮上预加7次。你在“point”变量中使用的值为0,这意味着当你按下btwpluse时,它只是在添加0?。将数字从0更改为1。此外,您的代码并不表示您实际上正在将tv更改为SharedReference变量。我修改了我的答案来告诉你们,我不明白我是怎么做的:else如果(btnpluse==v){point++;tv.setText(“+point”);所以每次点击都会添加1,当我做点时++会添加1,就像我展示BTN时应用程序所做的一样。如果我将电视更改为1,2,3,4,5……很公平,试试我答案中的内容。忘记点变量的东西。