[英]After calling setEnabled(false) button still can be pressed one more time
When clicking a button I immediately call setEnabled
to false however this does not disable the button and I have to press again to disable. 单击按钮时,我立即将
setEnabled
为false,但这不会禁用该按钮,因此我必须再次按下才能禁用。
I have tried to put setEnabled
in different places but with the same result. 我试图将
setEnabled
放在不同的位置,但结果相同。
chooseLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseLeft.setEnabled(false);
chooseRight.setEnabled(true);
docRefPosts.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if(!chooseRight.isEnabled())
docRefPosts.update("votesForRight", FieldValue.increment(-1));
docRefPosts.update("votesForLeft", FieldValue.increment(1));
}
}
});
}
});
chooseRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chooseRight.setEnabled(false);
docRefPosts.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if(!chooseLeft.isEnabled())
docRefPosts.update("votesForLeft", FieldValue.increment(-1));
docRefPosts.update("votesForRight", FieldValue.increment(1));
}
}
});
chooseLeft.setEnabled(true);
}
});
It should function as a radio button where once selected only one can be chosen at a time. 它应作为单选按钮,一旦被选中,一次只能选择一个。
You will need to use setClickable(false)
as well if you wish to negate button click 如果您希望否定按钮单击,则还需要使用
setClickable(false)
chooseLeft.setClickable(false);
chooseLeft.setEnabled(false);
chooseRight.setClickable(true);
chooseRight.setEnabled(true);
I found the answer and I only have a guess as to why it is working. 我找到了答案,但只能猜测为什么会起作用。 What I did was move all the code for the
OnCompleteListener
into a separate method. 我所做的就是将
OnCompleteListener
所有代码移到一个单独的方法中。 I think that the listener was running before the app could register setEnabled(false)
. 我认为在应用程序可以注册
setEnabled(false)
之前,侦听器已在运行。 If anyone has any thoughts on this reasoning I would love to hear. 如果有人对此推理有任何想法,我很想听听。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.