【问题标题】:SharedPreferences save bg color errorSharedPreferences 保存背景颜色错误
【发布时间】:2017-01-19 10:46:12
【问题描述】:

您好,我在保存背景颜色时遇到问题,不知道为什么他没有得到代码:

        mlinearLayout.setBackgroundColor(Color.colourValue);

他用红色说“颜色值”。我需要你的帮助,这里的 java 代码:

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,prefs;
    private int progress;


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

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);
        SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE);
        String colourValue = sp.getString("colourValue", "WHITE");
        mlinearLayout.setBackgroundColor(Color.colourValue);

        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) ;
        tv.setText(textValue);
        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);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","RED");
            editor.commit();

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","GREEN");
            editor.commit();
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putString("colourValue","BLUE");
            editor.commit();


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

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

这是问题的图像

【问题讨论】:

  • 你可以用作`Color.WHITE`
  • 对不起,我不明白你的意思
  • Color."your color" 返回整数值,您正在传递字符串。所以就像我上面提到的那样尝试
  • ittry it bro 它仍然不保存我点击的最后一个颜色:/ 对不起,我的 noobi im 学生刚刚开始

标签: java android android-sharedpreferences


【解决方案1】:

用给定的更新替换您的代码:

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,prefs;
    private int progress;


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

        mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);
        SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE);
        int colourValue = sp.getInt("colourValue", Color.WHITE);
        mlinearLayout.setBackgroundColor(colourValue);

        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) ;
        tv.setText(textValue);
        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);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.RED);
            editor.commit();

        }
        else if (btngreen == v){
            mlinearLayout.setBackgroundColor(Color.GREEN);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.GREEN);
            editor.commit();
        }
        else if (btnblue == v){
            mlinearLayout.setBackgroundColor(Color.BLUE);
            SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit();
            editor.putInt("colourValue", Color.BLUE);
            editor.commit();


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

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

希望能帮到你!

【讨论】:

  • 兄弟,首先为你的 helo sec 我确定我做错了什么,因为它仍然无法工作,请查看图片并告诉我我做错了什么:s29.postimg.org/a3za3syhz/image.jpgs23.postimg.org/60srsuqjv/image.jpg
  • 我仍然遇到同样的问题,我确定我做错了什么,比如你放“context.getSharedPreferences”,但他不让我放“context”看看s23.postimg.org/foanpk8m3/334.jpg
  • 这里可以直接从activity调用getSharedPreference()。查看更新!
  • 确定了“colorvalue”的问题修复了thx,但他仍然不保存bg颜色我是保存代码中的问题吗?请看看我的保存代码是否正确:s29.postimg.org/jrfuev57r/5522.jpg
  • 尝试重新安装应用程序!这可能是因为之前您将其保存为String,现在保存为int
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多