我希望启动按钮启动时间,停止按钮在此时完全停止,然后重置按钮将时钟重新设置为“00.00”。每次我点击重置它都会重新启动时间。我只是想让它重置回“00.00”。谢谢你的帮助。继承我的代码:
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class TimeLogActivity extends ActionBarActivity {Chronometer focus;
Button start, stop, reset;
private Toolbar toolbar;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_log);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
start = (Button) findViewById(R.id.timeLogStartButton);
stop = (Button) findViewById(R.id.timeLogStopButton);
reset = (Button) findViewById(R.id.timeLogResetButton);
focus = (Chronometer) findViewById(R.id.timeLogChronometer);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
focus.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
focus.stop();
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
focus.setBase(SystemClock.elapsedRealtime());
focus.start();
}
});
}
@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_time_log, 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);
}
}
答案 0 :(得分:3)
这些线你应该能够将计时器重置回00:00
focus.setBase(SystemClock.elapsedRealtime());
focus.stop();
答案 1 :(得分:2)
重置焦点并开始计数
focus.setBase(SystemClock.elapsedRealtime());
然后你可以说
focus.stop();
我将如何重置并在00:00停止焦点。
Idk更好的方式。
答案 2 :(得分:0)
focus.setBase(SystemClock.elapsedRealtime());
SystemClock.elapsedRealtime()!= 00:00
答案 3 :(得分:0)
如果您依次调用这两行,您将会完成
focus.setBase(SystemClock.elapsedRealtime());
focus.stop();
它的作用是,再次启动计时码表并迅速将其停止。 这对我有用,似乎是目前最好的解决方案。