如何从java隐藏/显示TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38060002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:58:28  来源:igfitidea点击:

How to hide/show TextView from java

javaandroidxml

提问by Rajubhai Rathod

I have TextView in one of my layout. I want keep it hide and only want make visible one time on button click, how can I do it ? My view is like below. Thanks

我的布局之一中有 TextView。我想让它隐藏,只想在单击按钮时显示一次,我该怎么做?我的观点如下。谢谢

        <TextView
            android:layout_marginBottom="16dp"
            android:layout_marginRight="8dp"
            android:id="@+id/textAuthorSign"
            android:layout_gravity="right"
            android:text="- ABJ Abdul Kalam"
            android:textStyle="italic"
            android:textSize="16sp"
            android:typeface="serif"
            android:visibility="invisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

Thanks

谢谢

采纳答案by Shank

I think you want a toggle (As stated by the question title)

我想你想要一个切换(如问题标题所述)

XML File:

XML文件:

     <Button
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/self_destruct"
         android:onClick="hide" />

     <TextView
         android:layout_marginBottom="16dp"
         android:layout_marginRight="8dp"
         android:id="@+id/textAuthorSign"
         android:layout_gravity="right"
         android:text="- ABJ Abdul Kalam"
         android:textStyle="italic"
         android:textSize="16sp"
         android:visibility="invisible"
         android:typeface="serif"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

Java:

爪哇:

 public void hide(View view) {

     TextView txtView = (TextView)findViewById(R.id.textAuthorSign);

     //Toggle
     if (txtView.getVisibility() == View.VISIBLE)
        txtView.setVisibility(View.INVISIBLE);
     else 
        txtView.setVisibility(View.VISIBLE);

     //If you want it only one time
     //txtView.setVisibility(View.VISIBLE);

 }

回答by BananaBuisness

You can use setVisibilitymethod as of this example:

您可以使用setVisibility本示例中的方法:

TextView tv = (TextView)findViewById(R.id.textAuthorSign);
tv.setVisibility(View.VISIBLE);

Will make your view visible and View.invisablewill make your view invisible.

将使您的视图可见,View.invisable并使您的视图不可见。

You can also do View.GONEand then the TextView won't take any space on the layout...

您也可以这样做View.GONE,然后 TextView 不会在布局上占用任何空间...

回答by Mr. Negi

First get the reference to the textView:

首先获取对textView的引用:

  TextView textView = (TextView)findViewById(R.id.textViewName);

Then

然后

  textView.setVisibility(TextView.VISIBLE);