代表朋友发布这个,我没有 android SDK 来测试这个,如果它非常明显,我很抱歉:
尝试使用新行上的时间戳更新 TextView:
viewText = viewText + "\n"+ String.format("%d:%02d:%02d", minutes, seconds,millis);
但无论如何,似乎总是以同一条线结束。
下面的代码:
package com.example.polyrhythm;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView showResults, countDown;
long start, stop;
String sResults = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bStart = (Button) findViewById(R.id.startButton);
Button bStop = (Button) findViewById(R.id.rhythmOne);
showResults = (TextView) findViewById(R.id.resultTextView);
// countDown = (TextView) findViewById(R.id.countDownTextView);
showResults.setSingleLine(false);
bStart.setOnClickListener(this);
bStop.setOnClickListener(this);
start = 0;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.startButton:
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// countDown.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
// countDown.setVisibility(View.GONE);
start = System.currentTimeMillis();
}
}.start();
break;
case R.id.rhythmOne:
stop = System.currentTimeMillis();
if (start != 0) {
String viewText = "";
long result = stop - start;
int millis = (int) result;
int seconds = (int) result / 1000;
int minutes = seconds / 60;
millis = millis % 100;
seconds = seconds % 60;
viewText = viewText
+ "\n"
+ String.format("%d:%02d:%02d", minutes, seconds,
millis);
showResults.setText(viewText);
}
break;
}
}
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/rhythmOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="----------------" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rhythmOne"
android:layout_centerHorizontal="true"
android:text="Start" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/startButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result\n"
android:lines="2" />
</LinearLayout>
</ScrollView>
</RelativeLayout>