单击按钮时如何更改图像背景? 我希望能够让用户按下按钮,然后将电影添加到数据库中,当他再次按下它时,它会将其从SQL表中取出,并让他们能够区分它是否在数据库或者没有显示的图像。
这是我到目前为止所做的:
isPressed = false;
ImageView imageView = new ImageView(this);
final FloatingActionButton favoriteButton = new FloatingActionButton.Builder(this)
.setContentView(imageView)
.setLayoutParams(params)
.setBackgroundDrawable(R.drawable.favorite_unselected)
.setTheme(FloatingActionButton.THEME_DARK)
.setPosition(FloatingActionButton.POSITION_TOP_RIGHT)
.build();
favoriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If button already pressed then I want it un-favorite
if(isPressed)
favoriteButton.setBackgroundResource(R.drawable.favorite_unselected);
else
favoriteButton.setBackgroundResource(R.drawable.favorite_selected);
isPressed = !isPressed;
}
});