嗨,我是Java和android领域的初学者,但我想进行一个类似于设置的活动:同时更改背景色和字体颜色。
package com.example.bartek.smb;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class OptionActivity extends AppCompatActivity {
private Button but1;
private Button but2;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
view = this.getWindow().getDecorView();
but1 = findViewById(R.id.rednut);
but2 = findViewById(R.id.greenbut);
}
public void goRed(View v){
but1.setTextColor(Color.RED);
view.setBackgroundResource(R.color.red);
}
public void goGreen(View v){
but2.setTextColor(Color.GREEN);
view.setBackgroundResource(R.color.green);
}
}
因此,我做了2个按钮来更改字体颜色和背景,但它仅适用于当前活动,并且在关闭设备后消失。我已经阅读了共享首选项,但我不知道如何实现。
答案 0 :(得分:1)
public class OptionActivity extends AppCompatActivity {
private String MY_PREFS_NAME = "sharedPref";
private Button but1;
private Button but2;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
view = this.getWindow().getDecorView();
but1 = findViewById(R.id.rednut);
but2 = findViewById(R.id.greenbut);
}
public void goRed(View v){
//Set shared preference to red
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("backgroundColor", Color.RED);
editor.commit();
but1.setTextColor(Color.RED);
view.setBackgroundResource(R.color.red);
}
public void goGreen(View v){
//Set shared preference to green
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("backgroundColor", Color.GREEN);
editor.commit();
but2.setTextColor(Color.GREEN);
view.setBackgroundResource(R.color.green);
}
}
只要需要检索值,就可以使用它。
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int backgroundColor = prefs.getInt("backgroundColor",0);
but2.setBackgroundColor(backgroundColor);
答案 1 :(得分:0)
像下面的onCreate()一样合并SharedPreferences
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
view = this.getWindow().getDecorView();
but1 = findViewById(R.id.rednut);
but2 = findViewById(R.id.greenbut);
if (prefs != null){
String s = prefs.getString("button1colour");
String st = prefs.getString("button2colour");
if (s.equals("red")){
but1.setTextColor(Color.red);
view.setBackgroundResource(R.color.red);
}
if (st.equals("green")) {
but2.setTextColor(Color.GREEN);
view.setBackgroundResource(R.color.green);
}
}
}
public void goRed(View v){
but1.setTextColor(Color.RED);
view.setBackgroundResource(R.color.red);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("botton1colour", red);
editor.commit();
}
public void goGreen(View v){
but2.setTextColor(Color.GREEN);
view.setBackgroundResource(R.color.green);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("botton2colour", green);
editor.commit();
}
}