开发者

What is the difference between the int used by View.VISIBLE and normal ints?

If I want to toggle the visibility of a TextView, I can use View.VISIBLE or View.INVISIBLE

According to the Android Docu开发者_如何学Gomentation, VISIBLE is 0 and INVISIBLE is 1.

But it doesn't work if I use setvisibility(0)

Why does View.VISIBLE work but not 0?


Checking the source code is always a valid option with Android. One thing that is immediately apparent is that INVISIBLE is not 1:

/**
 * This view is visible.  Use with {@link #setVisibility}.
 */
public static final int VISIBLE = 0x00000000;

/**
 * This view is invisible, but it still takes up space for layout purposes.
 * Use with {@link #setVisibility}.
 */
public static final int INVISIBLE = 0x00000004;

However, VISIBLE is indeed 0, so using a literal 0 should work. All setVisibility() really does is delegate to setFlags() with the number you pass it and VISIBILITY_MASK, which is 0x0C (12).


These int values can change all the time and that's why you need to be careful when using the numeric as opposed to enum-like parameter (I know it not an enum... just saying).

if you really want to know the value behind those parameters use:

hello.setText(Integer.toString(View.INVISIBLE))

with hello being a TextView.

in this case, the answer is 4 (.GONE is 8)


Best way:

private void setViewVisiblity(int visiblity){
        Button b = findViewById(R.id.btn);
        b.setVisibility(visiblity);
}

// for visible: 
setViewVisiblity(View.VISIBLE)

// for invisible: 
setViewVisiblity(View.INVISIBLE)

// for gone: 
setViewVisiblity(View.GONE)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜