gpt4 book ai didi

java - 更改 float 操作按钮背景

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:15 26 4
gpt4 key购买 nike

我正在使用 float 操作按钮,单击时其图像会发生变化。我有一个标志,并将其存储在共享首选项中。当为 true 时图标变为 like.png,当为 false 时图标变为 dislike.png。

我想根据标志变量更改其图标,但当应用程序启动时,其图标是默认的 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);

}


}

最佳答案

试试这个代码:

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);
}


}

关于java - 更改 float 操作按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com