Android:为什么 setVisibility(View.GONE);或 setVisibility(View.INVISIBLE);不工作

发布于 2024-12-03 17:04:17 字数 775 浏览 0 评论 0原文

我希望我的 DatePicker 和按钮在开始时不可见。当我按下魔术按钮时,我想 setVisibility(View.Visible);

这里的问题是,当我 setVisibility(View.GONE)setVisibility(View.INVISIBLE) 没有任何变化并且组件仍然可见时。

final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);

btn2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        TextView txt2 = (TextView) findViewById(R.id.txt2);
        txt2.setText("You selected " + dp2.getDayOfMonth()
            + "/" + (dp2.getMonth() + 1) + "/" + dp2.getYear());
    }
});

I want my DatePicker and the button to be invisible in the begining. And when I press my magic button I want to setVisibility(View.Visible);

The problem here is when I setVisibility(View.GONE) or setVisibility(View.INVISIBLE) nothing changes and the component is still visible.

final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);

btn2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        TextView txt2 = (TextView) findViewById(R.id.txt2);
        txt2.setText("You selected " + dp2.getDayOfMonth()
            + "/" + (dp2.getMonth() + 1) + "/" + dp2.getYear());
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

匿名的好友 2024-12-10 17:04:17

我看到很多事情都是错误的。对于初学者来说,您没有定义魔术按钮,也没有针对它的事件处理程序。

另外,您不应该使用:

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE); 

仅使用两者之一。来自 Android 文档

View.GONE 该视图是不可见的,并且不占用任何空间
布局目的。

View.INVISIBLE 该视图是不可见的,但它仍然
出于布局目的而占用空间。

在您的示例中,您将使用 View.INVISIBLE 赋值覆盖 View.GONE 赋值。


尝试将:替换

final DatePicker dp2 = new DatePicker(this)

为:

DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);  

对于其他小部件类似:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

        final DatePicker dp2 = new DatePicker(this);
        final Button btn2 = new Button(this);
        final Button magicButton = new Button(this);
        final TextView txt2 = new TextView(TestActivity.this);

        dp2.setVisibility(View.GONE);
        btn2.setVisibility(View.GONE);
        btn2.setText("set Date");

        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                txt2.setText("You selected "
                    + dp2.getDayOfMonth() + "/" + (dp2.getMonth() + 1) 
                    + "/" + dp2.getYear());
            }
        });

        magicButton.setText("Magic Button");
        magicButton.setOnClickListener(new View.OnClickListener()    
            public void onClick(View arg0) {
                dp2.setVisibility(View.VISIBLE);
                btn2.setVisibility(View.VISIBLE);
            }
        });

    ll.addView(dp2);
    ll.addView(btn2);
    ll.addView(magicButton);
    ll.addView(txt2);

    setContentView(ll);
}

I see quite a few things wrong. For starters, you don't have your magic button defined and there is no event handler for it.

Also you shouldn't use:

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE); 

Use only one of the two. From Android documentation:

View.GONE This view is invisible, and it doesn't take any space for
layout purposes.

View.INVISIBLE This view is invisible, but it still
takes up space for layout purposes.

In your example, you are overriding the View.GONE assignment with the View.INVISIBLE one.


Try replacing:

final DatePicker dp2 = new DatePicker(this)

with:

DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);  

Similarly for other widgets:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

        final DatePicker dp2 = new DatePicker(this);
        final Button btn2 = new Button(this);
        final Button magicButton = new Button(this);
        final TextView txt2 = new TextView(TestActivity.this);

        dp2.setVisibility(View.GONE);
        btn2.setVisibility(View.GONE);
        btn2.setText("set Date");

        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                txt2.setText("You selected "
                    + dp2.getDayOfMonth() + "/" + (dp2.getMonth() + 1) 
                    + "/" + dp2.getYear());
            }
        });

        magicButton.setText("Magic Button");
        magicButton.setOnClickListener(new View.OnClickListener()    
            public void onClick(View arg0) {
                dp2.setVisibility(View.VISIBLE);
                btn2.setVisibility(View.VISIBLE);
            }
        });

    ll.addView(dp2);
    ll.addView(btn2);
    ll.addView(magicButton);
    ll.addView(txt2);

    setContentView(ll);
}
故乡的云 2024-12-10 17:04:17

今天,我遇到了一个场景,我正在执行以下操作:

myViewGroup.setVisibility(View.GONE);

在下一帧上,我正在其他地方执行 if 检查该视图的可见性状态。你猜怎么着?以下条件通过:

if(myViewGroup.getVisibility() == View.VISIBLE) {
    // this `if` was fulfilled magically
}

放置断点,您可以看到,可见性变为 GONE,但在下一帧,它神奇地变成 VISIBLE。我试图理解这到底是怎么发生的。

事实证明,该视图应用了一个动画,这在内部导致视图将其可见性更改为 VISIBLE,直到完成动画:

public void someFunction() {
    ...
    TransitionManager.beginDelayedTransition(myViewGroup);
    ...

    myViewGroup.setVisibility(View.GONE);
}

如果您进行调试,您将看到 myViewGroup > 确实将其可见性更改为 GONE,但在下一帧,它会再次变得可见,以便运行动画。

因此,如果遇到这种情况,请确保在动画视图过程中没有执行 if 检查。

您可以通过 View.clearAnimation 删除视图上的所有动画()

Today I had a scenario, where I was performing following:

myViewGroup.setVisibility(View.GONE);

Right on the next frame I was performing an if check somewhere else for visibility state of that view. Guess what? The following condition was passing:

if(myViewGroup.getVisibility() == View.VISIBLE) {
    // this `if` was fulfilled magically
}

Placing breakpoints you can see, that visibility changes to GONE, but right on the next frame it magically becomes VISIBLE. I was trying to understand how the hell this could happen.

Turns out there was an animation applied to this view, which internally caused the view to change it's visibility to VISIBLE until finishing the animation:

public void someFunction() {
    ...
    TransitionManager.beginDelayedTransition(myViewGroup);
    ...

    myViewGroup.setVisibility(View.GONE);
}

If you debug, you'll see that myViewGroup indeed changes its visibility to GONE, but right on the next frame it would again become visible in order to run the animation.

So, if you come across with such a situation, make sure you are not performing an if check in amidst of animating the view.

You can remove all animations on the view via View.clearAnimation().

带刺的爱情 2024-12-10 17:04:17

你可以把它看作是CSS样式的可见性和可见性。展示。

<div style="visibility:visible; display:block">
    This is View.VISIBLE : Content is displayed normally.
</div>

<div style="visibility:hidden; display:block">
    This is View.INVISIBLE : Content is not displayed, but div still takes up place, but empty.
</div>

<div style="display:none">
    This is View.GONE : Container div is not shown, you can say the content is not displayed.
</div>

You can think it as a CSS style visibility & display.

<div style="visibility:visible; display:block">
    This is View.VISIBLE : Content is displayed normally.
</div>

<div style="visibility:hidden; display:block">
    This is View.INVISIBLE : Content is not displayed, but div still takes up place, but empty.
</div>

<div style="display:none">
    This is View.GONE : Container div is not shown, you can say the content is not displayed.
</div>
高跟鞋的旋律 2024-12-10 17:04:17

首先查看您的代码:

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);

在这里您将两个可见性设置为同一字段,这就是问题所在。
我为该示例演示提供了一个示例

First see your code:

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);

Here you set both visibility to same field so that's the problem.
I give one sample for that sample demo

指尖上的星空 2024-12-10 17:04:17

就我而言,我发现只需在将可见性设置为“GONE”之前清除视图上的动画即可。

dp2.clearAnimation();
dp2.setVisibility(View.GONE);

我遇到了类似的问题,我在两个视图之间切换,其中一个视图必须始终以 GONE 开头 - 但是当我再次显示视图时,即使 setVisibility,它也会显示在第一个视图上(GONE) 被调用。在将视图设置为“GONE”之前清除动画是有效的。

In my case I found that simply clearing the animation on the view before setting the visibility to GONE works.

dp2.clearAnimation();
dp2.setVisibility(View.GONE);

I had a similar issue where I toggle between two views, one of which must always start off as GONE - But when I displayed the views again, it was displaying over the first view even if setVisibility(GONE) was called. Clearing the animation before setting the view to GONE worked.

耳根太软 2024-12-10 17:04:17

View.GONE 该视图是不可见的,并且不占用任何布局空间。

View.INVISIBLE 该视图是不可见的,但出于布局目的,它仍然占用空间。

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);

View.GONE This view is invisible, and it doesn't take any space for layout purposes.

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);
自控 2024-12-10 17:04:17

这是写给尝试了所有答案但仍然失败的人的。扩展pierre的答案。如果您使用动画,则将可见性设置为 GONEINVISIBLEinvalidate() 永远不会起作用。尝试下面的解决方案。
`

btn2.getAnimation().setAnimationListener(new Animation.AnimationListener() {
     @Override
     public void onAnimationStart(Animation animation) {
     }
     @Override
     public void onAnimationEnd(Animation animation) {
         btn2.setVisibility(View.GONE);
         btn2.clearAnimation();
     }
     @Override
     public void onAnimationRepeat(Animation animation) {
     }
});

`

This is for someone who tried all the answers and still failed. Extending pierre's answer. If you are using animation, setting up the visibility to GONE or INVISIBLE or invalidate() will never work. Try out the below solution.
`

btn2.getAnimation().setAnimationListener(new Animation.AnimationListener() {
     @Override
     public void onAnimationStart(Animation animation) {
     }
     @Override
     public void onAnimationEnd(Animation animation) {
         btn2.setVisibility(View.GONE);
         btn2.clearAnimation();
     }
     @Override
     public void onAnimationRepeat(Animation animation) {
     }
});

`

枫以 2024-12-10 17:04:17

View.GONE 使视图不可见,而不占用视图在布局中的空间。
View.INVISIBLE 使视图不可见,但仍然占用空间。

您首先在同一个视图上使用 GONE,然后使用 INVISIBLE。因为代码是按顺序执行的,所以视图首先变为 GONE,然后它被仍然占用空间的 INVISIBLE 类型覆盖。

您应该在按钮上添加按钮侦听器,并在 onClick() 方法内部使视图可见。根据我的说法,这应该是您的 onCreate() 方法中的逻辑。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
    final Button btn2 = (Button) findViewById(R.id.btnDate2);
    final Button btn3 = (Button) findViewById(R.id.btnVisibility);

    dp2.setVisibility(View.INVISIBLE);
    btn2.setVisibility(View.INVISIBLE);

    bt3.setOnClickListener(new View.OnCLickListener(){ 
    @Override
    public void onClick(View view)
    {
        dp2.setVisibility(View.VISIBLE);
        bt2.setVisibility(View.VISIBLE);
    }
  });
}

我认为这应该很容易实现。希望这有帮助。

View.GONE makes the view invisible without the view taking up space in the layout.
View.INVISIBLE makes the view just invisible still taking up space.

You are first using GONE and then INVISIBLE on the same view.Since, the code is executed sequentially, first the view becomes GONE then it is overridden by the INVISIBLE type still taking up space.

You should add button listener on the button and inside the onClick() method make the views visible. This should be the logic according to me in your onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
    final Button btn2 = (Button) findViewById(R.id.btnDate2);
    final Button btn3 = (Button) findViewById(R.id.btnVisibility);

    dp2.setVisibility(View.INVISIBLE);
    btn2.setVisibility(View.INVISIBLE);

    bt3.setOnClickListener(new View.OnCLickListener(){ 
    @Override
    public void onClick(View view)
    {
        dp2.setVisibility(View.VISIBLE);
        bt2.setVisibility(View.VISIBLE);
    }
  });
}

I think this should work easily. Hope this helps.

(り薆情海 2024-12-10 17:04:17

我也面临着同样的问题,如果假设特定片段在不同的屏幕上膨胀,则 if 语句内设置的可见性模式有可能无法根据我们的需要发挥作用,因为条件可能在膨胀时被重置在我们的应用程序中多次。

就我而言,我必须根据在另一个片段(父片段)中单击的按钮来更改一个片段(子片段)中的可见性模式。因此,我将 buttonClicked 布尔值存储在父片段的变量中,并将其作为参数传递给子片段中的函数。因此,子片段中的可见性模式会根据通过参数接收的布尔值进行更改。但随着这个子片段在各个屏幕上膨胀,即使我使用 View.GONE 将其隐藏,可见性模式也会不断重置。

为了避免这种冲突,我在子片段中声明了一个静态布尔变量,每当从父片段接收到该布尔值时,我都会将其存储在静态变量中,然后根据子片段中的该静态变量更改可见性模式。

这为我解决了这个问题。

I was also facing the same issue, if suppose that particular fragment is inflated across various screens, there is a chance that the visibility modes set inside the if statements to not function according to our needs as the condition might have been reset when it is inflated a number of times in our app.

In my case, I have to change the visibility mode in one fragment(child fragment) based on a button clicked in another fragment(parent fragment). So I stored the buttonClicked boolean value in a variable of parent fragment and passed it as a parameter to a function in the child fragment. So the visibility modes in the child fragments is changed based on that boolean value that is received via parameter. But as this child fragment is inflated across various screens, the visibility modes kept on resetting even if I make it hidden using View.GONE.

In order to avoid this conflict, I declared a static boolean variable in the child fragment and whenever that boolean value is received from the parent fragment I stored it in the static variable and then changed the visibility modes based on that static variable in the child fragment.

That solved the issue for me.

秋千易 2024-12-10 17:04:17

我曾经遇到过同样的问题。更改可见性后调用 invalidate 几乎总是有效,但在某些情况下却无效。我不得不作弊并将背景设置为透明图像并将文本设置为“”。

I had the same problem once. Calling invalidate after changing visibility almost always works, but in some cases it doesn't. I had to cheat and set the background to a transparent image and set the text to "".

怪我闹别瞎闹 2024-12-10 17:04:17

因为您将可见性设置为 true 或 false。
尝试一下
setVisible(0) 为可见 true 。
和 setVisible(4) 为可见 false。

Because you set visibility either true or false.
try that
setVisible(0) to visible true .
and setVisible(4) to visible false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文