我有一个计时器,想要暂停时记录。
这是我的计时器:
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
SetReturnTime(finalTime);
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);
//txt.setText("oxy="+oxy);
}
};
int sss=0;
public void SetReturnTime(long t){
sss = (int) (t/1000);
}
public long ReturnTime(){
return sss;
}
这些是开始/暂停按钮:
startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
startcount_now = true;
}
});
pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
});
将使用时间的类:
Button btnCalSpeed;
private EditText distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed_input);
distance = (EditText) findViewById(R.id.txtDistance);
btnCalSpeed = (Button) findViewById(R.id.btnCalSpeed);
btnCalSpeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String distanceToString = distance.getText().toString();
int IntDistance = new Integer(distanceToString).intValue();
//int timeTraveled = (int) (finalTime / 1000);
double Avgspeed = IntDistance/2;
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP| Gravity.LEFT, 0, 0);
toast.makeText(SpeedInput.this,"Speed = "+ ReturnTime(), toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_speed_input, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
然而,不知何故,当我显示sss
时,值为0.有什么方法可以解决这个问题吗?我想存储最后时间(以毫秒为单位),并在单击“暂停”按钮时将其更改为秒。
答案 0 :(得分:0)
/ *试试这个,我不确定* /
public static int sss=0;
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
sss=SetReturnTime(finalTime);
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);
//txt.setText("oxy="+oxy);
}
};
public int SetReturnTime(long t){
sss = (int) (t/1000);
return sss;
}
public long ReturnTime(){
return sss;
}
pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
Log.e("getPausedTime",""+sss);
}
});
答案 1 :(得分:0)
/ *方法一:您要发送到目标类的当前班级* /
Intent i=new Intent(ActivityA.this,ActivityB.class);
i.putExtra("sss",""+sss);
startActivity(i);
/ * onCreate方法中的目标类* /
Intent intent = getIntent();
String sss = intent.getStringExtra("sss");
Log.e("ValueOfsss",""+sss);
/ *方法2:在您的目标类(ActivityB)上只需执行此操作SourceClassName是ActivityA * /
Log.e("ValueOfsss",""+ActivityA.sss);