每隔5秒间隔隐藏并显示可见按钮

时间:2022-09-25 21:18:20

Can it possible to make a button on screen which automatically visible and gone every 5sec interval? By using this

是否可以在屏幕上制作一个自动可见并且每隔5秒间隔消失的按钮?通过使用这个

b.setVisibility(View.VISIBLE);

we can visible and

我们可以看到和

b.setVisibility(View.GONE); 

we can hide it.But I can't manage to make it by usin the time interval. Any idea?please share.

我们可以把它藏起来。但是我无法在时间间隔内成功。有什么想法吗?请分享。

2 个解决方案

#1


2  

Use this

用这个

new CountDownTimer(9000000, 5000) {

 public void onTick(long millisUntilFinished) {
     if(b.getVisibility() == View.GONE)
      b.setVisibility(View.VISIBLE);
     else
      b.setVisibility(View.GONE);
 }

 public void onFinish() {
   //Restart timer if you want.
 }
}.start();

#2


4  

There are a few different ways, one is a Handler and Runnable:

有几种不同的方式,一种是Handler和Runnable:

public class Example extends Activity {
    private Handler mHandler = new Handler();
    private Runnable alternate = new Runnable() {
        public void run() {
            // Alternate visible and not
            mHandler.postDelayed(alternate, 5000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mHandler.postDelayed(alternate, 5000);
    }
}

#1


2  

Use this

用这个

new CountDownTimer(9000000, 5000) {

 public void onTick(long millisUntilFinished) {
     if(b.getVisibility() == View.GONE)
      b.setVisibility(View.VISIBLE);
     else
      b.setVisibility(View.GONE);
 }

 public void onFinish() {
   //Restart timer if you want.
 }
}.start();

#2


4  

There are a few different ways, one is a Handler and Runnable:

有几种不同的方式,一种是Handler和Runnable:

public class Example extends Activity {
    private Handler mHandler = new Handler();
    private Runnable alternate = new Runnable() {
        public void run() {
            // Alternate visible and not
            mHandler.postDelayed(alternate, 5000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mHandler.postDelayed(alternate, 5000);
    }
}