简体   繁体   English

View.VISIBLE和常规int使用的int有什么区别?

[英]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 如果要切换TextView的可见性,可以使用View.VISIBLEView.INVISIBLE

According to the Android Documentation , VISIBLE is 0 and INVISIBLE is 1. 根据Android文档 ,VISIBLE为0,INVISIBLE为1。

But it doesn't work if I use setvisibility(0) 但是如果我使用setvisibility(0)则不起作用

Why does View.VISIBLE work but not 0? 为什么View.VISIBLE不能工作但不能0?

Checking the source code is always a valid option with Android. 检查源代码始终是Android的有效选项。 One thing that is immediately apparent is that INVISIBLE is not 1: 显而易见的一件事是, INVISIBLE 不是 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. 但是, VISIBLE确实为0,因此使用文字0应该可行。 All setVisibility() really does is delegate to setFlags() with the number you pass it and VISIBILITY_MASK , which is 0x0C (12). setVisibility()实际上所做的全部是将传递给它的数字和VISIBILITY_MASK委托给setFlags() ,它是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). 这些int值可以随时更改,这就是为什么在使用数字而不是像枚举之类的参数时要特别小心(我知道这不是枚举……只是说)。

if you really want to know the value behind those parameters use: 如果您真的想知道这些参数背后的值,请使用:

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

with hello being a TextView. 你好是一个TextView。

in this case, the answer is 4 ( .GONE is 8) 在这种情况下,答案是4( .GONE是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)

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

相关问题 Android setVisibility(View.Visible)不适用于布局 - Android setVisibility(View.Visible) not working on a layout startB.setVisibility(View.VISIBLE)在Android Studio上崩溃 - startB.setVisibility(View.VISIBLE) crashes on Android Studio public int和Java中int有什么区别? - What is the difference between public int and int in Java? 源文件夹和(普通)文件夹之间有什么区别 - What is the difference between a source folder and a (normal) folder 传递给视图解析器的普通字符串与在 spring mvc 中传递重定向:/字符串有什么区别? - what is difference between normal string passing to view resolver and passing with redirect:/string in spring mvc? 在view.setVisibility(View.GONE)和view.setVisibility(View.VISIBLE)之后刷新RelativeLayout - RelativeLayout refresh after view.setVisibility(View.GONE) and view.setVisibility(View.VISIBLE) 我想从ViewModel与我的View(Activity)进行交互。 从ViewModel使用View.VISIBLE - I want to interact with my View(Activity) from ViewModel. Using View.VISIBLE from ViewModel 从 View.GONE 状态到 View.VISIBLE 的 setVisibility 无法正常工作 - setVisibility from View.GONE state to View.VISIBLE not working properly buffer(int)和buffer()有什么区别? - What's the difference between buffer(int) and buffer()? 这个脚本中int和Integer有什么区别? - What is the difference between int and Integer in this script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM