【问题标题】:How to set Timer when button clicked?单击按钮时如何设置计时器?
【发布时间】:2015-08-31 05:47:21
【问题描述】:

我的意思是就像秒表一样,当点击按钮时,计时器一直打开,直到按下停止按钮

startbutton= (Button)findViewById(R.id.black1);
  startbutton.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
              //Start the timer
                                         }                
            });

 stopbutton= (Button)findViewById(R.id.black1);
 stopbutton.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
              //Stop the timer
                                         }                
            });

第二个问题,

如果计时器显示 90 秒,如何让它在屏幕上显示图像视图或按钮?像一些 if 语句使按钮可见,每个计时器计数为 90 秒(90、180、270 等),他会将按钮可见性设置为可见。

之前谢谢。

【问题讨论】:

标签: java android timer


【解决方案1】:

在您的xml 中使用计时器

<Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer" />

在你的java

Chronometer focus = (Chronometer) findViewById(R.id.chronometer1);

startButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        focus.start();
        setVisibilityTimerOn(); //Second Question Solution
    }
});

stopButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        focus.stop();
        setVisibilityTimerOff();
    }
});

第二个问题如果你想打开/关闭某些按钮/ImageView 的 VISIBILITY 设置一个处理程序

//Declare these variable 
private Handler handler;
private Runnable updateView;


private void setVisibilityTimerOn(){
     timeHandler = new Handler(); //it's better if you declare this line in onCreate (becuase if user press stopButton first before pressing startButton error will occur as handler was never initialized and you try calling removeCallback function)
     updateView = new Runnable() {
         public void run() {
              someImageView.setVisibility(View.VISIBLE);
         }
     };
     handler.postDelayed(updateView ,90000);//this will be on after 90 second
}

private void setVisibilityTimerOff(){
    handler.removeCallbacks(updateView);
}

【讨论】:

  • 是的,这很好,但仍然没有回答我在 L-X 中的第二个问题
  • 第二个问题没看懂,能详细点吗?
  • 我的意思是这样的,如果 Timer 已经 90 秒,那么做一些 if 语句来设置按钮的可见性。有可能这样做吗?
  • 总是乐于提供帮助:)
【解决方案2】:

这就像秒表计时器,间隔为 1 毫秒。

    Handler handler= new Handler();

1. click to start   (code)


  Runnable runnable = new Runnable() {
                       @Override
                       public void run() {
        //put your code to be executed on within every interval
                           handler.postDelayed(this, 1); 
                       }
                    };

        handler.postDelayed(runnable, 5); //start after 5 seconds 

 2. click to stop (code)
     handler.removeCallbacksAndMessages(null); 

它应该工作。 谢谢

【讨论】:

    猜你喜欢
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2014-02-28
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多