我想为这个计时器制作一个毫秒的进度条,但它不起作用 你能帮助我吗?也许它很简单,但如果点击开始(button1)
我的应用程序崩溃了1:
package com.example.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private CountDownTimer mCountDownTimer;
int i=0;
private ProgressBar pb;
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;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
int milliseconds = (int) (finalTime % 1000);
2:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTimer = (TextView) findViewById(R.id.textView1);
startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
});
3:
pauseButton = (Button) findViewById(R.id.button2);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
});
}
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
seconds = seconds % 60;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
pb.setProgress(milliseconds);
textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
}
也许你可以帮助我。 Thx Jonah
答案 0 :(得分:0)
你的应用程序正在崩溃,因为我想这行
pb.setProgress(milliseconds);
我看着它并清理了一下。现在这有一个用于textView的工作秒表。虽然有一个进度条在你的应用程序中无缘无故地运行。虽然我没有清楚地理解你的问题。希望这可以帮助您开始您的项目。
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private CountDownTimer mCountDownTimer;
int i=0;
private ProgressBar pb;
private TextView textTimer;
private Button startButton, pauseButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
int milliseconds = (int) (finalTime % 1000);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTimer = (TextView) findViewById(R.id.textView1);
pb = (ProgressBar) findViewById(R.id.progressBar); // here is the key
startButton = (Button) findViewById(R.id.button1);
pauseButton = (Button) findViewById(R.id.button2);
startButton.setOnClickListener(this);
pauseButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
textTimer.setText("b1 here showing startTime" + startTime);
break;
case R.id.button2:
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
textTimer.setText("b2 showing timeSwap" + timeSwap);
break;
}
}
public Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
seconds = seconds % 60;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
pb.setProgress(milliseconds); // because you call this
textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}