一个简单的倒计时器,用于更新double变量的值,然后将值显示在文本框的末尾。我用整数完成了它并且它工作正常,但是当我使用双值时它只是从文本框的末尾消失。这种情况有没有原因?
使用的代码:
PSICount = new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timer2.setText("seconds remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
if(alti >= 33000){
Random r = new Random();
double Low = 0.2 ;
double High = 0.5;
double randomValue = Low + (High - Low) * r.nextDouble();
pressure = pressure - randomValue;
pressureT.setText("Pressure: "+ pressure + " PSI");
}
PSICount.start();
}
};
PSICount.start();
这是计时器,如果另一个变量的值大于33000,它会每5秒触发一次。一旦该变量达到33000,文本框(末尾的占位符值为10.0)就不会显示任何值在末尾。我是否需要做一些事情来使用textViews的双打?
谢谢
我的全班:
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class flight_engineer extends AppCompatActivity {
int fuel = 100;
int alti = 10000;
int speed = 50;
double over_heat = 600;
double pressure = 10.0;
CountDownTimer fuelCount;
CountDownTimer PSICount;
CountDownTimer spike;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight_engineer);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
final TextView fuelT = (TextView) findViewById(R.id.fuelText);
final TextView pressureT = (TextView) findViewById(R.id.presText);
final TextView altitudeT = (TextView) findViewById(R.id.altText);
final TextView speedT = (TextView) findViewById(R.id.speedText);
final TextView over_heatingT = (TextView) findViewById(R.id.heatText);
final TextView info = (TextView) findViewById(R.id.infoText);
final TextView timer = (TextView) findViewById(R.id.timer);
final TextView timer2 = (TextView) findViewById(R.id.timer2);
fuelT.setTypeface (typeface);
pressureT.setTypeface (typeface);
altitudeT.setTypeface (typeface);
speedT.setTypeface (typeface);
over_heatingT.setTypeface (typeface);
info.setTypeface(typeface);
fuelT.setText("Fuel: "+fuel);
pressureT.setText("Pressure: "+ pressure+ " PSI");
altitudeT.setText("Altitude: "+alti+" ft");
speedT.setText("Speed: "+speed+" MPH");
over_heatingT.setText("System Heat: "+over_heat+" °C");
Button speedPlus = (Button) findViewById(R.id.speedPlus);
speedPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
fuelCount = new CountDownTimer(2000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
fuelCount.start();
fuel--;
fuelT.setText("Fuel: "+fuel);
over_heat=over_heat + 4;
fuelT.setText("Fuel: "+fuel);
over_heatingT.setText("System Heat: "+over_heat+" °C");
Random r = new Random();
int Low = 443;
int High = 872;
int Result = r.nextInt(High - Low) + Low;
alti = alti + Result;
altitudeT.setText("Altitude: "+alti+" ft");
}
};
fuelCount.start();
PSICount = new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timer2.setText("seconds remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
if(alti >= 33000){
Random r = new Random();
double Low = 0.2 ;
double High = 0.5;
double randomValue = Low + (High - Low) * r.nextDouble();
pressure = pressure - randomValue;
pressureT.setText("Pressure: "+ pressure + " PSI");
}
PSICount.start();
}
};
PSICount.start();
spike = new CountDownTimer(30000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
spike.start();
over_heat = over_heat + 50;
over_heatingT.setText("System Heat: "+over_heat+" °C");
alti = alti + 1000;
altitudeT.setText("Altitude: "+alti+" ft");
if(speed < 300 || speed > 300) {
Random r = new Random();
int Low = -50;
int High = 50;
int Result = r.nextInt(High - Low) + Low;
speed = speed + Result;
speedT.setText("Speed: " + speed + " MPH");
}
}
};
spike.start();
}
}