Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

前端 未结 11 1999
[愿得一人]
[愿得一人] 2020-12-02 12:51

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

相关标签:
11条回答
  • 2020-12-02 13:20

    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) {
         }
    });
    

    `

    0 讨论(0)
  • 2020-12-02 13:23

    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

    0 讨论(0)
  • 2020-12-02 13:28

    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.

    0 讨论(0)
  • 2020-12-02 13:31

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 13:33

    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);
    
    0 讨论(0)
  • 2020-12-02 13:33

    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 "".

    0 讨论(0)
提交回复
热议问题