开发者

Android: How to make a TextView visible for a defined time at runtime

I have a Layout with a TextView, and I want to make the text or the View itself to be presented for a defined time at run time. How can I do that?

I tried with Animation: I've placed the TextView inside an Animation tag in the main.xml, but when i use:

animation = AnimationUtils.loadAnimation(this, R.id.msg_anim);

and later:

animation.startNow();

I get an exception.

So How to m开发者_如何学Goake the text or the TextView visible for a second?

Thank You.


You can use handlers to handle timed UI elements during runtime.

TextView myTV;
Handler uiHandler = new Handler();
Runnable makeTextGone = new Runable(){
   @Override
   public void run(){
      myTv.setVisibility(View.GONE);
   }
};

@Override
public void onCreate(Bundle icicle){
   .... code ....
   myTv = (TextView) findViewById(R.id.myTextView);
   uiHandler.postDelayed(makeTextGone, 1000);
   ... code ...
}

The call to uiHandler.postDelayed(makeTextGone, 1000); will call the runnable only once after one second. Put it immediately after you make the text visible and it should disappear after a second.


You need to make sure that your activity is instantiated before you call any animations or resources.

Make sure you are calling this AFTER super.onCreate();

Hope this helped.


You might be getting the exception because you are trying to start the animation in onCreate().

As per the documentation, the recommended place to start animations is in onResume(). If that's not the problem, post some more information about your Exception (like the stack trace)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜