Hide TextView after some time in Android

こ雲淡風輕ζ 提交于 2020-01-12 14:11:17

问题


I want to hide TextView after some time interval say 3 seconds. I googled and found some code and I tried code as shown below but It is not working.

Please tell me what is the wrong with this ?

tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
    public void run() {
        tvRQPoint.setText("+0");
    }
}, 3000);

One more thing, how to remove timeout ? As I am using this on click event of ListView, If user clicks on one option and then clicks on second option, then as 3 seconds got over (after clicked on first option), It does not show second option for 3 seconds.


回答1:


try View INVISIBLE or GONE like:

tvRQPoint.postDelayed(new Runnable() {
public void run() {
    tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);

Set View visibility with view.setVisibility(View.INVISIBLE|View.VISIBLE|View.GONE);




回答2:


How about hiding your text view with some animation?

  int delayMillis = 3000;
  Handler handler = new Handler();
  final View v = tvRQPoint; // your view
  handler.postDelayed(new Runnable() { 
    @Override
    public void run() {
       TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
       animate.setDuration(500);
       animate.setFillAfter(true);
       v.startAnimation(animate);
       v.setVisibility(View.GONE);

    }, delayMillis);



回答3:


What you are trying to do is ok but after three seconds you want to hide the textview so use setVisibility

tvRQPoint.setText("+0");
    tvRQPoint.postDelayed(new Runnable() {
        public void run() {
            tvRQPoint.setVisibility(View.INVISIBLE);
        }
    }, 3000);



回答4:


Try this-

Handler handler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
tvRQPoint.setText("+0");
handler = new Handler();
handler.postDelayed(csRunnable, 3000); 
}

Runnable csRunnable =new Runnable() 
{      
@Override
public void run() 
{
tvRQPoint.setVisibility(View.INVISIBLE);    
}
};



回答5:


try this...

public class MainActivity extends Activity   {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView tv=(TextView)findViewById(R.id.tv);
    tv.setText("+0");
    tv.postDelayed(new Runnable() {
        public void run() {
            tv.setVisibility(View.INVISIBLE);
        }
    }, 3000);
}
 }



回答6:


You are setting the text in run() method. you can hide the text in two ways

View.INVISIBLE - give the space for the textview and hide its content

View.GONE - remove the space for textview and hide its content

so call

tvRQPoint.setVisibility(View.INVISIBLE);

                   (or)

tvRQPoint.setVisibility(View.GONE);



回答7:


hope it's work:

tvRQPoint.setText("+0");
Timer timer = new Timer();
    timer.schedule(new TimerTask() {

       public void run() {

          tvRQPoint.setVisibility(View.GONE);

       }

    },3000);


来源:https://stackoverflow.com/questions/22194761/hide-textview-after-some-time-in-android

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