java 禁用 OnClickListener Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17601343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:26:38  来源:igfitidea点击:

Disable OnClickListener Android

javaandroidonclick

提问by Phoenix

I have an OnClickListeneron an image in my app. It allows the user to skip to a different part of the app if desired. The way the app runs, they can only use it 3 times.

OnClickListener我的应用程序中有一个图像。如果需要,它允许用户跳到应用程序的不同部分。该应用程序的运行方式,他们只能使用 3 次。

My issue is, I want to get fancy pants. So I added an R.anim.fade_outanimation to make the image fade out after all 3 times were used. I am using a counter decreased by one in another method.

我的问题是,我想要一条漂亮的裤子。所以我添加了一个R.anim.fade_out动画,使图像在使用 3 次后淡出。我正在使用以另一种方法减一的计数器。

The issue is, when the original method is recalled, it throws a Null Reference Exceptionbecause it can't find the image to set the OnClickListener. I tried wrapping it in an If/Else Ifstatement:

问题是,当调用原始方法时,它抛出 aNull Reference Exception因为它找不到图像来设置OnClickListener. 我试着把它包装在一个If/Else If声明中:

if(skipsAllowed > 0){

            skipButton.setOnClickListener(new OnClickListener(){

                public void onClick(View v){

                    if(skipsAllowed > 0){
                        try {
                            skippedPage();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            });

        }else  if(skipsAllowed == 0){
            skipFadeOut = AnimationUtils.loadAnimation(null, android.R.anim.fade_out);
            skipButton.startAnimation(skipFadeOut);
        }

This still didn't work. Any ideas on how to stop this?

这仍然没有奏效。关于如何阻止这种情况的任何想法?

I instantiate the ImageViewat the start of every new call to this Activity, should I be placing that inside my If/ElseIf?

ImageView在每次新调用 this 时实例化Activity,我应该把它放在我的If/ElseIf 中吗?

回答by msysmilu

Simple: skipButton.setOnClickListener(null)

简单的: skipButton.setOnClickListener(null)

回答by Androiderson

If skipsAllowed == 0unregister onClickListener

如果skipsAllowed == 0注销onClickListener

See Remove an onclick listener

请参阅删除 onclick 侦听器

By the way, you should review your code, we usually set a click listeneronly once.

顺便说一句,你应该检查你的代码中,我们通常可以设置一个点击监听,只有一次

回答by RicardoM

You are getting an exception because you are passing null contextto loadAnimation.

您收到异常,因为您将空上下文传递给loadAnimation.

You can get application context with : getApplicationContext()

您可以通过以下方式获取应用程序上下文: getApplicationContext()

And after animation start you should set you button to INVISIBLEto completely hide the skip button.

动画开始后,您应该将按钮设置为INVISIBLE以完全隐藏跳过按钮

Animation skipFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out);
        skipButton.startAnimation(skipFadeOut);
        skipButton.setVisibility(View.INVISIBLE);