简体   繁体   中英

TextView visible invisible using one button in Android

I create a button and TextView when I press the button to visible the TextView its working good but my question was when same button pressed invisible the TextView how can i do this? This is my code:

  <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        />
    <TextView
        android:id="@+id/pas_rules"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:text="welcome"
        android:visibility="gone"/>

main activity:

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            textview.setVisibility(View.VISIBLE);

        }
    });
 button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {

        textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        // which is just like:
        /*
        if(textview.getVisibility() == View.VISIBLE)
             textview.setVisibility(View.GONE);
        else
             textview.setVisibility(View.VISIBLE);
        */
    }
});

you can achieve this by checking the visibility of the view:-

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {

        if(textview.getVisibility()==View.GONE)
        {
        textview.setVisibility(View.VISIBLE);
       }
      else
       {
       textview.setVisibility(View.GONE);
       }

    }
});

Another Way

 <Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="Button_Click"
    android:clickable="true"
    />
<TextView
    android:id="@+id/pas_rules"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:text="welcome"
    android:visibility="gone"/>

//Now Declare Button_Click Function in your Java Class

public void Button_Click(View i)
{

    textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
    // Do your Code in here 
} 

You can check whether your Textview is visible or not if you click on the button. If it's visible, you can remove the visibility, if not, you can make it visible. You can find a solution here: How to check TextView Visibility using IF You have to use the textview.getVisibility() method and check it to View.VISIBLE. If it is visible you have to set your textview invisible: textview.setVisibility(View.GONE);

Your code could look like:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (textview.getVisibility()==View.VISIBLE){
                textview.setVisibility(View.GONE);
            }
            else{
                textview.setVisibility(View.VISIBLE);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM