سؤال

im a student and i'm working on my first andorid app. I want to create e button that can hide and show a text, so i've wrote this on fragment :

<TextView android:id="@+id/textView1" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"
     android:layout_marginTop="36dp" 
     android:text="@string/t1"
     android:visibility="invisible"/>

  <ImageButton android:id="@+id/imageButton2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
          android:layout_below="@+id/textView1"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="38dp"
          android:onClick="click_button1"
          android:src="@drawable/pippo"
         android:text="@string/s1" />

and this on main:

 public void click_button1 (View view){ 
        TextView textview=(TextView)findViewById(R.id.textView1); 
           textview.setVisibility(View.VISIBLE);
     }

on main what can i use to change visibility?!

هل كانت مفيدة؟

المحلول

Try out for visible and invisible the TextView on your Button click.

 public void click_button1 (View view){ 
        TextView textview=(TextView)parent.findViewById(R.id.textView1); 
          if(!textview.isShown())  //Check if the view is currently visible or not.
             textview.setVisibility(View.VISIBLE);
         else
             textview.setVisibility(View.INVISIBLE);
     }

نصائح أخرى

Make the view View.GONE on your xml layout. Because it won't take the memory in your activity. You can verify this by hierarchyviewer. Once you click the button make the button visibility gone and make your text view visible.

Use

boolean bool=false;
         TextView textview;
         ViewGroup parent;

             parent = (ViewGroup) view.getParent();
             textview=(TextView)parent.findViewById(R.id.textView1);

                public void click_button1 (View view){ 

              if(bool){
                      textview.setVisibility(View.VISIBLE);
                      bool=true;
              }else{
                    textview.setVisibility(View.INVISIBLE);
                    bool=false;
                }
                  }

You should use:

textview.setVisibility(View.GONE);

or

textview.setVisibility(View.INVISIBLE);

Google Documentation says:

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top