我需要在8.47秒后更改textview 8次,然后再更改8次,但仅在8.00秒后更改。 请帮助,因为我不确定如何编码。
我试过寻找代码,但它没有帮助如何做到这一点。 这是我到目前为止所拥有的。
public class Twenty_Meter_Test extends ActionBarActivity implements View.OnClickListener {
Chronometer myChronometer;
int level = 1;
int shuttle = 1;
int updates = 0;
String levels = level + "." + shuttle;
TextView textView = (TextView) findViewById(R.id.levelcount);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweney__meter__test);
myChronometer = (Chronometer) findViewById(R.id.chronometer);
(findViewById(R.id.start_twenty)).setOnClickListener(this);
(findViewById(R.id.stop_twenty)).setOnClickListener(this);
}
public void onClick (View view) {
textView.setText(levels);
switch (view.getId()) {
case R.id.start_twenty: {
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
break;
}
case R.id.stop_twenty: {
myChronometer.stop();
break;
}
}
}
public void updateTextView () {
if (updates < 8) {
updates++;
level++;
textView.postDelayed(new Runnable() {
@Override
public void run() {
level++;
textView.setText(levels);
updateTextView();
}
}, 8470);
}
level = 0;
shuttle = 2;
if (updates > 8 && updates < 16) {
updates++;
textView.postDelayed(new Runnable() {
@Override
public void run() {
level++;
updateTextView();
}
}, 8000);
}
}
答案 0 :(得分:0)
不要使用Chronometer,试试这个
*创建全局变量和对象
private Handler mHandler = new Handler();
private long timeUnformatted = 0L;
private long timeInMilliseconds = 0L;
private long timeSwapBuff = 0L;
private long updatedTime = 0L;
*使用runnable
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - timeUnformatted;
updatedTime = timeSwapBuff + timeInMilliseconds;
seconds = (int) (updatedTime / 1000);
minutes = seconds / 60;
seconds = seconds % 60;
ms = (int) (updatedTime % 1000);
if (seconds == 8) {
// do something
}
}
};
在onCreate()中启动计时器添加
mHandler.postDelayed(updateTimerMethod, 0);
停止计时器添加
mHandler.removeCallbacks(updateTimerMethod);
在onPause()中添加
timeSwapBuff += timeInMilliseconds;
mHandler.removeCallbacks(updateTimerMethod);
*在onResume()中再次开始