如何设置定时器,当单击按钮? [英] How to set Timer when button clicked?

查看:139
本文介绍了如何设置定时器,当单击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是像stopwach,当单击按钮计时器是直到pssed停止按钮$ P $

I mean is like stopwach, when button clicked the Timer is on until the stop button pressed

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秒如何使它显示在屏幕上的ImageView或按钮?像一些if语句,使按钮可见每个定时器计数至90秒(90,180,270,等..),他将按钮的可见性设置为可见。

if timer show 90 second how to make it show imageview or button in screen? like some if statement to make button visible each timer count to 90 second (90, 180, 270, etc..) he will set button visibility to visible.

谢谢了。

推荐答案

使用天文台在 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的建立一个处理程序

Second Question If you want to turn VISIBILITY on/off of some button/ImageView set up a handler

//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);
}

这篇关于如何设置定时器,当单击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆