SharedPreferences如何保存文本视图和bg颜色

时间:2017-01-15 13:40:42

标签: java android android-studio android-sharedpreferences

嗨,所有的第一件事我一整天都在为这个寻找答案,我没有发现任何东西,我试图保存一个简单的事情,我需要保存背景颜色和"点"用户对button i的压力将解释用户需要按下更改button的{​​{1}}和background color后增加button +1的用户。

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

这里的代码: XML:

button

,这是java代码:

 <?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(); } } } 上尝试SharedPreferences但是在我关闭并返回之后我什么都不知道我知道我做错了什么但我不知道是什么。 所有的时间和帮助

1 个答案:

答案 0 :(得分:0)

将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)

在每个更改背景颜色的按钮中,输入此代码并更改我的变量&#39; COLOR&#39;到&#39;蓝色&#39;红色&#39;等

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);
相关问题