我一直在尝试在我的应用程序中构建一个秒表,它开始依靠点击开始按钮。我希望它从秒开始计数,然后是分钟,然后是小时,但问题是我无法计算小时但我可以计算我不想要的毫秒数。可以在启动按钮上单击应用程序获取当前系统时间并在停止点击时只计算并打印启动和停止点击之间的间隔?
这是我的活动类
package com.example.rimapps.stopwatch;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button but1,but2;
long MillisecondTIme,StarTime,TimeBuff,UpdateTime=0L;
Handler handler;
int MilliSeconds,Seconds,Minutes,Hour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
but1=(Button)findViewById(R.id.buttonstart);
but2=(Button)findViewById(R.id.buttonstop);
handler = new Handler();
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Date d=new Date();
//SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
//String currentDateTimeString=sdf.format(d);
//textView.setText(currentDateTimeString);
StarTime=SystemClock.uptimeMillis();
handler.postDelayed(runnable,0);
//reset.setEnabled(false);
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MillisecondTIme=0L;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;
MilliSeconds = 0 ;
textView.setText("00:00:00");
}
});
}
public Runnable runnable=new Runnable() {
@Override
public void run() {
MillisecondTIme=SystemClock.uptimeMillis()-StarTime;
UpdateTime=TimeBuff+MillisecondTIme;
Seconds=(int)(UpdateTime/1000);
Minutes=Seconds/60;
Seconds=Seconds%60;
MilliSeconds=(int)(UpdateTime%1000);
textView.setText("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds));
handler.postDelayed(this,0);
}
};
}
这是我的xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rimapps.stopwatch.MainActivity">
<TextView
android:text="00:00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="50dp"
android:textStyle="bold"
android:textColor="#009688"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="47dp"
android:id="@+id/buttonstart"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonstop"
android:layout_marginRight="89dp"
android:layout_marginEnd="89dp"
android:layout_marginTop="140dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
在我学习一本书时,我也遇到了秒表编码问题,这是我使用的代码示例。
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours=seconds/3600;
int minutes=(seconds%3600)/60;
int secs=seconds%60;
String time = String.format("%d:%02d:%02d",hours,minutes,secs);
timeView.setText(time);
if(running)
{
seconds++;
}
handler.postDelayed(this,1000);
}
});
也理解Handler
类,处理程序将始终立即运行代码(就像永远的每秒一样)所以你必须添加一个布尔值,知道它是否正在运行,然后将其设置为你的按钮,如果{{1}单击按钮,您必须将运行设置为true。 Handler将永远像arduino的着名start
方法一样永远运行(它永远在运行)。
答案 1 :(得分:0)
使用具有clearMessage而不是postRunnable
的sendMessage这次我会为你写的(大多数是关于骆驼案的修复惯例):
停止功能位于第二个按钮动作侦听器
package com.example.rimapps.stopwatch;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button but1,but2;
long millisecondTIme,starTime,timeBuff,updateTime=0L;
Handler handler;
int milliSeconds,seconds,minutes,hour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
but1=(Button)findViewById(R.id.buttonstart);
but2=(Button)findViewById(R.id.buttonstop);
handler = new Handler() {
public void handleMessage(Message what) {
millisecondTIme=SystemClock.uptimeMillis()-starTime;
updateTime=timeBuff+millisecondTIme;
seconds=(int)(UpdateTime/1000);
minutes=Seconds/60;
seconds=Seconds%60;
milliSeconds=(int)(updateTime%1000);
textView.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliSeconds));
handler.sendEmptyMessageDelayed(0, 1000); //repost in a loop
}
}
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
starTime=SystemClock.uptimeMillis();
handler.sendEmptyMessage(0); //Here send message with id 0
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
millisecondTIme=0L;
timeBuff = 0L ;
updateTime = 0L ;
seconds = 0 ;
minutes = 0 ;
milliSeconds = 0 ;
textView.setText("00:00:00");
handler.removeMessages(0); //Here clear all messages with same id
}
});
}
}