倒计时结束后设置可见性

时间:2018-03-13 10:42:07

标签: java android android-studio countdowntimer

尽管仔细搜索了其他用户的问题,但我似乎并不了解如何进行倒数计时器。我想做的就是在30秒后将GIEW的可见度设置为不接触屏幕。到目前为止,我已经做到了这一点:

public class StatusFragment extends Fragment {
    CountDownTimer countDownTimer;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        countDownTimer = new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                          //SET VISIBILITY TO VISIBLE
            }

            public void onFinish() {
                             //SET VISIBILITY TO GONE
            }
        }.start();


        cpHover.setOnClickListener(new OnClickListener() {

            //Should I countDownTimer.start()? It says there's an error
            @Override
            public void onClick(View view) {
                if(textBox.getVisibility()==View.GONE){
                    donutProgress.setVisibility(View.VISIBLE);
                    textBox.setVisibility(View.VISIBLE);
                    image.setVisibility(View.VISIBLE);


                }
                else if(textBox.getVisibility()==View.VISIBLE){
                    donutProgress.setVisibility(View.GONE);
                    textBox.setVisibility(View.GONE);
                    image.setVisibility(View.GONE);
                }
            }
        });

这个过程应该很简单。在onCreate()中设置30秒倒计时,每次用户单击cpHover时开始倒计时。当他们再次点击它时,它应该重新开始倒计时。有两种隐藏UI的方法:在屏幕可见时单击屏幕或在30秒内完全不点击。

提前致谢。

4 个答案:

答案 0 :(得分:1)

onCreate()

中添加此内容
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
                //set visibility as gone here
                if(textBox.getVisibility()==View.VISIBLE){
                donutProgress.setVisibility(View.GONE);
                textBox.setVisibility(View.GONE);
                image.setVisibility(View.GONE);
            }
  }
}, 30000);

答案 1 :(得分:0)

非常简单,只需按以下方式更改您的代码:

countDownTimer = new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
                      //SET VISIBILITY TO VISIBLE
           donutProgress.setVisibility(View.VISIBLE);
            textBox.setVisibility(View.VISIBLE);
            image.setVisibility(View. VISIBLE);
        }

        public void onFinish() {
                         //SET VISIBILITY TO GONE
            donutProgress.setVisibility(View.GONE);
            textBox.setVisibility(View.GONE);
            image.setVisibility(View.GONE);
        }
        }
    }.start();

您想在Click事件中启动CountDownTimer,然后在点击事件中添加上述代码

快乐的结合......

答案 2 :(得分:0)

更新您的代码:

     public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);             
                setUpCountDown();
                cpHover.setOnClickListener(new OnClickListener() {

                    //Should I countDownTimer.start()? It says there's an error
                    @Override
                    public void onClick(View view) {
                      if(countDownTimer != null) {
                        countDownTimer.cancel();
                      }
                      setUpCountDown();
                    }
                );
    }
    private void setUpCountDown() {
              countDownTimer = new CountDownTimer(30000, 1000) {

                    public void onTick(long millisUntilFinished) {
                                  //SET VISIBILITY TO VISIBLE
                          donutProgress.setVisibility(View.VISIBLE);
                          textBox.setVisibility(View.VISIBLE);
                          image.setVisibility(View.VISIBLE);
                    }

                    public void onFinish() {

                            donutProgress.setVisibility(View.GONE);
                            textBox.setVisibility(View.GONE);
                            image.setVisibility(View.GONE);

                    }
                }.start();
    }

答案 3 :(得分:-1)

您可以使用handler.postDelayed()或任何 view.postDelayed()。例如:cpHover.postDelayed()

    final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if(textBox.getVisibility()== View.VISIBLE){
                    donutProgress.setVisibility(View.GONE);
                    textBox.setVisibility(View.GONE);
                    image.setVisibility(View.GONE);
                }
            }
        }, 30000);
        cpHover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if(textBox.getVisibility()== View.VISIBLE){
                            donutProgress.setVisibility(View.GONE);
                            textBox.setVisibility(View.GONE);
                            image.setVisibility(View.GONE);
                        }
                    }
                }, 30000);
            }
        });

**你可以尝试这段代码,我希望它会有所帮助(Inside onCreate)**