我在一项活动中有两个计时器。一个是倒数计时器,另一个是秒表。
对于倒数计时器,我想修改我的代码,让用户输入倒计时的时间(以小时或分钟的格式,如果可能的话,不是秒),并重置按钮。目前,我的代码仅从代码中指定的设定金额开始倒计时。
另外,如果可能,我不确定,所以我问 - 当倒数计时器点击" 0"时,它会使手机振动或做某种通知(宁愿振动。)
对于秒表,我只需要让我的重置按钮也能正常工作。 这是计时器的代码
public class timers extends Activity {
/** Called when the activity is first created. */
Button btnStart, btnStop;
TextView textViewTime;
private TextView textTimer;
private Button startButton;
private Button pauseButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("00:03:00");
final CounterClass timer = new CounterClass(180000,1000);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
textTimer = (TextView) findViewById(R.id.textTimer);
startButton = (Button) findViewById(R.id.btnStart2);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
});
pauseButton = (Button) findViewById(R.id.btnPause2);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
textViewTime.setText("Completed.");
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
}
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Countdown Timer"
android:id="@+id/textView"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Stopwatch"
android:id="@+id/textView2"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_above="@+id/btnStart2" />
<TextView
android:id="@+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp"
android:textSize="25sp"
android:layout_below="@+id/textView"
android:textColor="#ffffb524"
android:text="00:00:00" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_below="@+id/textViewTime"
android:layout_toLeftOf="@+id/textViewTime" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@+id/textViewTime"
android:layout_toRightOf="@+id/textViewTime" />
<TextView
android:id="@+id/textTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFFFB524"
android:text="00:00:00"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="152dp"
android:textIsSelectable="true" />
<Button
android:id="@+id/btnPause2"
android:layout_width="90dp"
android:layout_height="45dp"
android:text="Pause"
android:layout_toEndOf="@+id/textTimer"
android:layout_toStartOf="@+id/resetButton2"
android:layout_below="@+id/btnStart2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<Button
android:id="@+id/btnStart2"
android:layout_width="90dp"
android:layout_height="45dp"
android:text="Start"
android:layout_toEndOf="@+id/btnPause2"
android:layout_marginTop="72dp"
android:layout_alignTop="@+id/textTimer"
android:layout_alignLeft="@+id/textTimer" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/resetButton2"
android:layout_toEndOf="@+id/textView2"
android:layout_above="@+id/btnPause2"
android:layout_toRightOf="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="@+id/setTime"
android:layout_toEndOf="@+id/btnStart"
android:layout_below="@+id/btnStart"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/resetButton"
android:layout_below="@+id/setTime"
android:layout_toRightOf="@+id/btnStart"
android:layout_toEndOf="@+id/btnStart" />
</RelativeLayout>
答案 0 :(得分:1)
一旦你在其上放置@your_id并使用findViewById(R.id.your_id)来获取视图,你应该能够从edittext中读取时间输入。然后你可以使用getText()来获取它的文本。
重置按钮类似于您已有的开始按钮。只需让它停止计时器并清除编辑文本视图。如果使用字段而不是局部变量,则应该在onclick方法中使用它。
绝对可以在达到零时振动手机。我会推荐一个通知。在此处阅读有关通知构建器的信息。
http://developer.android.com/reference/android/app/Notification.Builder.html