Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击按钮时如何设置计时器?_Java_Android_Timer - Fatal编程技术网

Java 单击按钮时如何设置计时器?

Java 单击按钮时如何设置计时器?,java,android,timer,Java,Android,Timer,我的意思是,就像stopwach一样,当按下按钮时,计时器就会打开,直到按下停止按钮 startbutton= (Button)findViewById(R.id.black1); startbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v){ //Start the timer

我的意思是,就像stopwach一样,当按下按钮时,计时器就会打开,直到按下停止按钮

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等),他将按钮可见性设置为可见


谢谢您。

在您的
xml

<Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer" />
第二个问题如果要打开/关闭某些按钮/图像视图的可见性,请设置处理程序

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

xml

<Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chronometer" />
第二个问题如果要打开/关闭某些按钮/图像视图的可见性,请设置处理程序

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

它的工作原理类似于秒表计时器,间隔为1ms

    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); 
它应该会起作用。
谢谢

这就像秒表计时器一样,间隔为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); 
它应该会起作用。
谢谢

请查看以下链接:谢谢给某个链接的人帮助但仍然没有回答我的第二个问题检查:或者请查看以下链接:谢谢给某个链接的人帮助但仍然没有回答我的第二个问题检查:或者是的,很好但仍然没有回答我的第二个问题这里的问题L-XI不明白你的第二个问题,你能详细解释一下吗?我的意思是,如果计时器已经90秒了,那么做一些if语句来设置按钮的可见性。有可能这样做吗?总是很乐意帮助:)是的,很好,但仍然没有回答我的第二个问题。L-XI不理解你的第二个问题,你能详细说明吗?我的意思是,如果计时器已经90秒了,那么做一些if语句来设置按钮可见性。这样做是可能的吗?总是乐于助人:)