Android View set to Visible and Gone in the same OnClick method, View never showing Visible

回眸只為那壹抹淺笑 提交于 2019-12-07 16:05:17

问题


Hi stackoverflow community,

I'm using Android API 14 on a droid 4.0.3 device.

In the Activity I've set a Button to display a TextView on the page while it is performing an action. After the action is performed, I want the TextView to disappear again.

button1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // make textview visible 
        textView1.setVisibility(View.VISIBLE);      

        // perform action
        System.out.println("perform action");

        // make textview disappear
        textView1.setVisibility(View.GONE);
    }
});

If I remove the part that makes the TextView disappear, the TextView appears at the top of the window as expected, but I want the TextView to appear for 1-2 seconds and then disappear.

At first I wondered if I needed to do more work than just performing a small action, so I tried adding a wait and printing out text, but none of that worked. The wait always called the exception, ending the activity, and when I printed out numbers 1-1000 the view was still permanently gone.

Is there a better way to make a TextView appear and disappear on an OnClick action?

Thanks for your help!


回答1:


Those commands are executed back-to-back. So in a technical sense, it may be visible but only for a millisecond or two. You need to distinguish when to make the view visible and when to hide it...


You said that you would like the TextView to "blink" in a sense, so let's use the Handler that is a part of every View to call a Runnable. This Runnable will simply hide the TextView a few moments later:

1) Set up a class variable:

Runnable hide = new Runnable() {
    @Override
    public void run() {
        textView1.setVisibility(View.GONE);
    }
};

2) Call the runnable in a few moments:

button1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // make textview visible for 1 second (1000 milliseconds)
        textView1.setVisibility(View.VISIBLE);      
        textView1.postDelayed(hide, 1000);
    }
});

(A Handler and Runnable does not block the UI thread.)




回答2:


here is example with handler (I have not compiled it!)

Handler handler = new Handler();

button1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // make textview visible 
        textView1.setVisibility(View.VISIBLE);      

        // perform action
        System.out.println("perform action");

        // make textview disappear
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
              textView1.setVisibility(View.GONE);
         }} , 2000);
    }
});



回答3:


Use an animation

public class MainActivity extends Activity {
     TextView txt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.btn_animation);

        txt = (TextView) findViewById(R.id.textviewtest);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                txt.clearAnimation();

                Animation fadeIn = new AlphaAnimation(0, 1);
                fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
                fadeIn.setDuration(1000);

                Animation fadeOut = new AlphaAnimation(1, 0);
                fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
                fadeOut.setStartOffset(1000);
                fadeOut.setDuration(1000);

                AnimationSet animation = new AnimationSet(false); //change to false
                animation.addAnimation(fadeIn);
                animation.addAnimation(fadeOut);

                animation.setAnimationListener( new AnimationListener() {

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        txt.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                        txt.setVisibility(View.VISIBLE);
                    }

                });
                txt.setAnimation(animation);
            }
        });
    }

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<Button
    android:id="@+id/btn_animation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Animation" />


<TextView
    android:id="@+id/textviewtest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:text="HelloWorld" />




回答4:


You should use an handler to run the bit of code that sets the Visibility to GONE. Perhaps Running a task at a specific time using postDelayed might help.



来源:https://stackoverflow.com/questions/13090630/android-view-set-to-visible-and-gone-in-the-same-onclick-method-view-never-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!