Android – 在点击后无法将按钮设置为setClickable(false)。

huangapple go评论94阅读模式
英文:

Android - Cannot setClickable(false) a button after click

问题

我有一个像开关一样的按钮,在点击后我尝试使用setClickable(false),以便只处理第一次点击(防止意外的双击/多次点击)。

这里有一个类似的代码:

  1. Button.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. Button.setClickable(false);
  5. //执行其他操作
  6. }
  7. });

然后最终,我在某个地方的代码会根据状态变量将可点击性重置为true,这样我就可以关闭按钮。

问题是,当我非常快速地点击按钮时,似乎后续的点击仍然被处理。
setClickable() 有延迟效果吗?

此外,我已经阅读了使用setEnabled(false) 的方法,但在我的情况下我不能使用它。我需要按钮仍然保持启用状态,但不能点击。

英文:

I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).

Here is a similar code:

  1. Button.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. Button.setClickable(false);
  5. //do other things
  6. }
  7. });

Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.

The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?

Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.

答案1

得分: 1

根据您的评论,您可能需要类似这样的代码:

  1. Boolean SWITCH_ON = false;
  2. Button.setOnClickListener(new OnClickListener() {
  3. public void onClick(View v) {
  4. if (!SWITCH_ON) {
  5. SWITCH_ON = true;
  6. }
  7. }
  8. });
  9. Button.setOnLongClickListener(new View.OnLongClickListener() {
  10. public boolean onLongClick(View v) {
  11. if (SWITCH_ON) {
  12. // 在这里执行长按时的任务...SWITCH_ON
  13. }
  14. return true;
  15. }
  16. });
英文:

Judging from your comment you probably need something like this.

  1. Boolean SWITCH_ON = false;
  2. Button.setOnClickListener(new OnClickListener() {
  3. public void onClick(View v) {
  4. if(!SWITCH_ON ){
  5. SWITCH_ON = true;
  6. }
  7. }
  8. });
  9. Button.setOnLongClickListener(new View.OnLongClickListener() {
  10. public boolean onLongClick(View v) {
  11. if(SWITCH_ON ){
  12. // do your task for long click here ...SWITCH_ON
  13. }
  14. return true;
  15. }
  16. });

答案2

得分: 0

你可以在你的onClick方法中使用button.setEnabled(false);来禁用按钮。

禁用的按钮不会触发onClick方法,当需要时你可以使用button.setEnabled(true);来重新启用它。

英文:

You can use button.setEnabled(false); within your onClick method to disable the button.

Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.

答案3

得分: 0

你可以添加另一个名为buttonEnabled的变量,或者其他类似的名称,并将其初始化为true。然后在点击事件中执行以下操作:

  1. Button.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. Button.setClickable(false);
  5. if(buttonEnabled) {
  6. //执行其他操作
  7. }
  8. buttonEnabled = false;
  9. }
  10. });

注意,如果你想要重新激活它,需要改变变量的值。

英文:

You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:

  1. Button.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. Button.setClickable(false);
  5. if(buttonEnabled) {
  6. //do other things
  7. }
  8. buttonEnabled = false;
  9. }
  10. });

Note that you need to change the variable to if you want to reactivate it

huangapple
  • 本文由 发表于 2020年9月1日 13:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63682090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定