简体   繁体   English

Android:隐藏动画按钮

[英]Android: Hide animating button

I'm a bit stuck again. 我有点卡住了。 This time i have an animated button, the animation is fading the button in and out. 这次我有一个动画按钮,动画使按钮淡入淡出。 What i want to achieve is that when i press the button it should disappear. 我想要实现的是,当我按下按钮时它应该消失。 Sounds easy enough, but what I've tried so far doesn't work, and I'm guessing it's because the button is animated. 听起来很容易,但是到目前为止我尝试过的都不起作用,我猜测是因为按钮是动画的。

I've tried setting the button.setVisibility(View.Invisible) in the onClick() listener.. My code is as follows: 我尝试在onClick()侦听器中设置button.setVisibility(View.Invisible)。我的代码如下:

Animation: fade.xml 动画:fade.xml

 <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.4" android:duration="1000" android:repeatCount="infinite" android:repeatMode="reverse" /> </set> 

I have a button declared in my layout xml called buttonStartTest and this is the code in the activity: 我在布局xml中声明了一个名为buttonStartTest的按钮,这是活动中的代码:

Button goButton = (Button) findViewById(R.id.buttonStartTest);

goButton.setVisibility(View.VISIBLE);
goButton.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade));
goButton.setBackgroundResource(R.drawable.roundedcorners);
goButton.setText("Start");
goButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // click action here
        Button goButton = (Button) findViewById(R.id.buttonStartTest);
        goButton.setVisibility(View.INVISIBLE);
    }
});

The problem is that the button doesn't disappear.. I can alter it in other ways in the onClick() though.. So my fellow coders, how do I go about hiding the button? 问题是按钮不会消失。.尽管我可以在onClick()中以其他方式对其进行更改。.所以我的编码员,我该如何隐藏按钮?

much love, Daniel 非常爱,丹尼尔

You should do a 你应该做一个

goButton.clearAnimation()

inside your on click listener, so that when you click on it, you will stop the animation, and then you can use 在您的点击监听器中,这样当您单击它时,您将停止动画,然后可以使用

goButton.setVisibility(View.Gone)

to fully hide it once and for all. 一劳永逸地完全隐藏它。

Looking your anim file, it's infinite, so the alpha value from your button will vary to 1-0.4 so you must first stop your animation: 看动画文件,它是无限的,因此按钮上的alpha值将变为1-0.4,因此必须首先停止动画:
goButton.clearAnimation();

Then you can hide your button: 然后,您可以隐藏按钮:

goButton.setVisibility(View.INVISIBLE)

Try this for me, if it works (which I think it will), then i'll explain. 请为我尝试一下,如果它有效(我认为可以),那么我会解释。

goButton.setVisibility(View.GONE);

The reason I believe is because setting it to invisible only makes the button transparent. 我相信是因为将其设置为不可见只会使按钮透明。 Your animation will override that. 您的动画将覆盖它。

I haven't had a chance to test this solution myself since I am not on my home PC, but this should work - Please let me know. 由于我不在家用PC上,因此我没有机会自己测试此解决方案,但这应该可以工作-请让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM