我是Android新编码的新手,我编写了这段代码来帮助我评估我的进度,并了解android中的编码。一旦按下开始按钮,计时器就会启动。我认为程序运行正常,每次调试。但是屏幕上没有定时器输出。
请帮助,谢谢 .XML文件
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.android.clockproject.MainActivity">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="START"
android:onClick="collectTime"/>
<TextView
android:id="@+id/digits_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="50sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
.Java文件
package com.example.android.clockproject;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import static android.R.attr.button;
import static android.R.attr.onClick;
public class MainActivity extends AppCompatActivity {
int hours = 0;
String hoursDisplay = "00";
int minutes = 0;
String minutesDisplay = "00";
int seconds = 0;
String secondsDisplay = "00";
int milliSecond = 0;
String millisecondDisplay = "0";
String fullTimeDisplay;
int loop = 7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* method collectTime is called when "start" button is pressed
*/
public void collectTime(View view) {
while (loop == 7) {
fullTimeDisplay = hoursDisplay + ":" + minutesDisplay + ":" + secondsDisplay + "." + millisecondDisplay;
displayMessage(fullTimeDisplay);
firstDigit();
}
}
/**
* method is called to increse hours
*
* @param hours
* @return
*/
private String forthDigit(int hours) {
hours = hours + 1;
if (hours == 13) {
hours = 0;
}
if (hours < 10) {
hoursDisplay = "0" + hours;
} else {
hoursDisplay = "" + hours;
}
return hoursDisplay;
}
/**
* method is called to increase minutes
*
* @param minutes
* @return
*/
private String thirdDigit(int minutes) {
minutes = minutes + 1;
if (minutes == 60) {
forthDigit(hours);
minutes = 0;
}
if (minutes < 10) {
minutesDisplay = "0" + minutes;
} else {
minutesDisplay = "" + minutes;
}
return minutesDisplay;
}
/**
* method called to increase seconds
*
* @return
*/
private int secondDigit() {
seconds = seconds + 1;
if (seconds == 60) {
thirdDigit(minutes);
seconds = 0;
}
if (seconds < 10) {
secondsDisplay = "0" + seconds;
} else {
secondsDisplay = "" + seconds;
}
return seconds;
}
/**
* method called to increase milliseconds
*/
private void firstDigit() {
milliSecond = milliSecond + 1;
if (milliSecond > 9) {
milliSecond = 0;
millisecondDisplay = "0";
secondDigit();
}
millisecondDisplay = "" + milliSecond;
}
/**
* This method displays the given fullTimeDisplay "the clock" on the screen.
*/
public void displayMessage(String fullTimeDisplay) {
TextView timeDisplayTextView = (TextView) findViewById(R.id.digits_display);
timeDisplayTextView.setText(fullTimeDisplay);
}
}
答案 0 :(得分:0)
您还没有发布您的xml布局文件,因此很难提供帮助。 首先,我尝试使用测试字符串从onCreate()调用displayMessage()函数。如果这不起作用,则问题出在您的xml布局中,您需要将其添加到您的问题中,以便我们看到错误。
编辑:
第一次没看到这个。 你使用一个阻塞主线程的while循环直到它完成,我不明白为什么它没有设置文本但是任何一种while循环都不是一个好的解决方案,因为你将来尝试运行的其他代码不会工作
我建议将collectTime()函数更改为:
public void collectTime(View view) {
// 30,000 milliseconds is 30 seconds, the 1000 is how many
milliseoncds to wait before calling the onTick
CountDownTimer timer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timeDisplayTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timeDisplayTextView.setText("done!");
// restarts the time to run 30 seconds again
timer.start();
}
}.start();
}