共享首选项保存Textview背景

时间:2016-11-26 20:41:59

标签: android sharedpreferences

我有三个按钮"红色","绿色"和"保存"和Textview。每当我按下" Red"或"格林"按钮Textview背景颜色将被更改。当我按"保存"我需要将更改的背景颜色保存到共享首选项。按钮。我需要打开一个活动改变背景颜色。我怎样才能做到这一点?

  

布局/ activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.gela.myexampleeeeeeeeeeeee.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:id="@+id/button_save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="59dp"
        android:onClick="onClick" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:background="@drawable/red"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="red"
        android:id="@+id/button_red"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp"
        android:onClick="onClick" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="green"
        android:id="@+id/button_green"
        android:layout_alignBottom="@+id/button_red"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="onClick" />
</RelativeLayout>
  

MainActivity.java

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button button_save, button_red, button_green;
    TextView textView;

    private SharedPreferences prefs;

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

        button_save = (Button)findViewById(R.id.button_save);
        button_red = (Button)findViewById(R.id.button_red);
        button_green = (Button)findViewById(R.id.button_green);
        textView = (TextView)findViewById(R.id.textView);

        prefs = getPreferences(MODE_PRIVATE);

        // what code for this?

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_red:
                textView.setBackgroundResource(R.drawable.red);
                break;
            case R.id.button_green:
                textView.setBackgroundResource(R.drawable.green);
                break;
            case R.id.button_save:
                prefs = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();

                //what code for this?


                break;

        }
    }
}

image

  

抽拉/红色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <gradient android:startColor="#FF4081" android:centerColor="#FF4081" android:endColor="#FF4081"/>
            <corners android:bottomLeftRadius="50dp" android:bottomRightRadius="50dp" android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
            <stroke android:width="2dp" android:color="#ffffff" />
            <padding android:left="15dp" android:top="2dp" android:right="15dp" android:bottom="2dp"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <gradient android:startColor="#4D000000" android:centerColor="#4D000000" android:endColor="#4D000000"/>
            <corners android:bottomLeftRadius="50dp" android:bottomRightRadius="50dp" android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
            <stroke android:width="2dp" android:color="#ffffff" />
            <padding android:left="15dp" android:top="2dp" android:right="15dp" android:bottom="2dp"/>
        </shape>
    </item>

</selector>
  

抽拉/绿色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <gradient android:startColor="#00e676" android:centerColor="#00e676" android:endColor="#00e676"/>
            <corners android:bottomLeftRadius="50dp" android:bottomRightRadius="50dp" android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
            <stroke android:width="2dp" android:color="#ffffff" />
            <padding android:left="15dp" android:top="2dp" android:right="15dp" android:bottom="2dp"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <gradient android:startColor="#4D000000" android:centerColor="#4D000000" android:endColor="#4D000000"/>
            <corners android:bottomLeftRadius="50dp" android:bottomRightRadius="50dp" android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
            <stroke android:width="2dp" android:color="#ffffff" />
            <padding android:left="15dp" android:top="2dp" android:right="15dp" android:bottom="2dp"/>
        </shape>
    </item>

</selector>

1 个答案:

答案 0 :(得分:0)

    public String StoreData(String yourKey, String colorName)
    {
       SharedPreferences sharedPref = context.getSharedPreferences("yourFileName", Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPref.edit();
       editor.putString(yourKey, colorName);
       editor.commit();
    }

    public String SaveData(String keyThatYouUsedToStore)
    {
       SharedPreferences sharedPref = context.getSharedPreferences("yourFileName",Context.MODE_PRIVATE);
       return sharedPref.getString(keyThatYouUsedToStore, "No String found!");
    }

首先 - 不要存储drawable。由于您的drawable似乎在运行时没有改变,因此存储它是一种过度杀伤。

相反,在我的示例中,您将名称存储在颜色的字符串中。根据存储和返回的字符串,您可以绑定适当的资源。您可以存储资源的整数(即R.id.button_red),但这需要单独研究,因为存储普通的R.id.button_red将是不可靠的。