我想喜欢兴趣 1,不喜欢兴趣 0
案例 R.id.floatingActionButton:
if() {floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.hearts));
addToInterested(postId, 1);
}else {
floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_fav));
addToInterested(postId, 0);
}
break;
方法内部 私有(private)无效addToInterested(字符串postId,int兴趣){
try {
PostLikeTask postLikeTask = new PostLikeTask(this, responseListener);
JSONObject params = new JSONObject();
params.put("userId", authKey);
params.put("postId", postId);
params.put("interest", interest);
postLikeTask.likePost(params);
} catch (Exception e) {
e.printStackTrace();
}
}
ResponseListener responseListener = new ResponseListener() {
@Override
public void onError(String errorMessage) {
Toast.makeText(getApplicationContext(), errorMessage.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(String message) {
Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_LONG).show();
}
};
}
最佳答案
您可以使用全局curInterest
来保存当前的兴趣状态。
或者检查 FloatingButton 的状态:
Drawable myDrawable = floatingButton.getDrawable();
if(floatingButton.getDrawable()==getResources().getDrawable(R.drawable. hearts)){
//Liked
}
但是,我看到 floatingButton
你改变了背景,不关心 addToInterested
状态成功或错误,当你的请求太快或有错误时,它可能会变成错误状态一个错误。
关于java - 如何在点击android上添加类似和不同条件按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278963/