【发布时间】:2021-08-17 17:21:13
【问题描述】:
我有这个 setOnClickListener ,它首先检查用户是否登录,然后如果用户登录将运行如下函数:
like.setOnClickListener {
val sharedPreference2 = context?.getSharedPreferences("isLogin", Context.MODE_PRIVATE)
val fbtoken = sharedPreference2?.getString("UserToken", "false")
if(fbtoken.equals("false"))
{
val builder = AlertDialog.Builder(context).create()
val optionDialog = AlertDialog.Builder(context)
val layoutInflator = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val dialogView = layoutInflator.inflate(R.layout.alert_reg, null)
val goreg = dialogView.findViewById<TextView>(R.id.confirmation_reg)
val cancel = dialogView.findViewById<TextView>(R.id.cancel_reg)
builder.setCancelable(false)
builder.setView(dialogView)
goreg?.setOnClickListener {
val intent = Intent(context, SignupActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
cancel?.setOnClickListener {
builder.dismiss()
}
builder.show()
}
else
{
postLike(position, it.context)
like.setImageResource(R.drawable.liked)
}
}
所以这个 setOnClickListener 首先会检查登录,然后在“else”中会在 API 中发布。
如果用户再次单击,我正在尝试使用上面使用的相同 setOnClickListener ,它将从 API 中删除它
我尝试将“else”替换为“if”和“else”,如下面的代码:
if (fbtoken.equals("true")){
postLike(position, it.context)
like.setImageResource(R.drawable.liked)
}else{
like.setImageResource(R.drawable.like)
deleteLike(position, it.context)
}
但现在它只从 API 中删除它
它类似于 IG 类似按钮,如果我第一次点击该按钮,它会发布类似的内容,如果我再次点击它会删除它。
我在这里做错了什么?
【问题讨论】:
标签: java android api kotlin onclicklistener