我正在使用浮动操作按钮,其图像在单击时会发生变化。我有一个标志,我将其存储在共享首选项中。当它是真的时,icon变为like.png,当它为false时,icon变为dislike.png。
我想根据flag变量更改其图标但是当应用程序启动时,它的图标是default,这是true.png
如何通过共享偏好设置图标?
public class ReadActivity extends AppCompatActivity {
private FloatingActionButton fab;
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
fab = (FloatingActionButton) findViewById(R.id.fab);
getRate(getWindow().getDecorView().getRootView());
if(flag==false){
fab.setImageResource(R.drawable.like);
}
else if(flag==true){
fab.setImageResource(R.drawable.dislike);
}
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getRate(v);
if(flag==false) {
rateUp();
Snackbar.make(v, "Vote +1", Snackbar.LENGTH_LONG).show();
saveRate(v, true);
fab.setImageResource(R.drawable.dislike);
}
else if(flag==true){
rateDown();
Snackbar.make(v, "Vote -1", Snackbar.LENGTH_LONG).show();
saveRate(v, false);
fab.setImageResource(R.drawable.like);
}
}
});
}
public void rateUp() {};
public void rateDown() {};
public void saveRate(View view, boolean flag){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(id, flag);
editor.commit();
}
public void getRate(View view){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
flag = sharedPreferences.getBoolean(id, false);
}
}
答案 0 :(得分:0)
试试这段代码:
public class ReadActivity extends AppCompatActivity {
private FloatingActionButton fab;
private string getSharedPreferencesId="getSharedPreferencesId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
fab = (FloatingActionButton) findViewById(R.id.fab);
boolean flag=getRate();
if(flag==false){
fab.setImageResource(R.drawable.like);
}
else if(flag==true){
fab.setImageResource(R.drawable.dislike);
}
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean flag=getRate();
if(flag==false) {
rateUp();
Snackbar.make(v, "Vote +1", Snackbar.LENGTH_LONG).show();
saveRate(true);
fab.setImageResource(R.drawable.dislike);
}
else if(flag==true){
rateDown();
Snackbar.make(v, "Vote -1", Snackbar.LENGTH_LONG).show();
saveRate(false);
fab.setImageResource(R.drawable.like);
}
}
});
}
public void rateUp() {};
public void rateDown() {};
public void saveRate(boolean flag){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getSharedPreferencesId, flag);
editor.commit();
}
public boolean getRate(){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
flag = sharedPreferences.getBoolean(getSharedPreferencesId, false);
}
}
答案 1 :(得分:0)
我对您的代码做了一些改进:
public class ReadActivity extends AppCompatActivity {
private FloatingActionButton fab;
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
fab = (FloatingActionButton) findViewById(R.id.fab);
flag = getRate();
if(flag)
fab.setImageResource(R.drawable.dislike);
else
fab.setImageResource(R.drawable.like);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = getRate();
if(flag) {
rateDown();
Snackbar.make(v, "Vote -1", Snackbar.LENGTH_LONG).show();
saveRate(false);
fab.setImageResource(R.drawable.like);
}
else{
rateUp();
Snackbar.make(v, "Vote +1", Snackbar.LENGTH_LONG).show();
saveRate(true);
fab.setImageResource(R.drawable.dislike);
}
}
});
}
public void rateUp() {};
public void rateDown() {};
public void saveRate(boolean flag){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(id, flag);
editor.commit();
}
public boolean getRate(){
SharedPreferences sharedPreferences = getSharedPreferences("Rates", MODE_PRIVATE);
boolean flag = sharedPreferences.getBoolean(id, false);
return flag;
}
}
另请检查您的activity_read.xml文件。如果您在xml FloatingActionButton上设置了图像,它将始终使用该图像作为默认值。
答案 2 :(得分:0)
我会告诉你逻辑。假设您有许多不同的内容,并且您希望保存每个内容的评级数据。
首先,您需要将每个内容的评级数据存储到某个位置(本地或远程数据库,SharedPreferences,文件等)。
启动ReadActivity后,
重要的是,您必须为每个内容设置不同的值。
修改强>
在使用getSharedPreferences()之前尝试使用getApplicationContext() 您也可以尝试使用editor.apply()而不是editor.commit();
commit()和apply()执行相同的工作,但有两个不同, apply()异步工作, commit()返回布尔值。
public void saveRate(View view, boolean flag){
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Rates", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(id, flag);
editor.apply();
}
public void getRate(View view){
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Rates", MODE_PRIVATE);
flag = sharedPreferences.getBoolean(id, false);
}