我是Android开发的新手.试图完成一些相当简单的事情 - 当计时器滴答时改变一些显示的文本.这是可能相关的代码:
CountDownTimer currentTimer;
Resources res;
TextView timerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
res = getResources();
timerText = (TextView) findViewById(R.id.timer_text);
}
@Override
protected void onStart() {
super.onStart();
//"Get ready" countdown
currentTimer = new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timerText.setText("" + (int)Math.ceil(millisUntilFinished / 1000.0));
}
@Override
public void onFinish() {
...
}
};
currentTimer.start();
}
Run Code Online (Sandbox Code Playgroud)
这在模拟的4.2.2设备上工作正常,但在4.1.2设备(物理和模拟)上,更改的TextView在倒计时进行时显示:

如果你不知道,这是数字5,4,3重叠.因此,当我为TextView设置新字符串时,会显示新字符串,但不会替换旧字符串.我的应用程序中使用的任何其他TextView都以相同的方式运行.
任何想法是什么问题以及如何解决它?
编辑:从XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ExerciseActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >
...
<TextView
android:id="@+id/timer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textIsSelectable="false"
android:hint="@string/timer_default" />
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这就是所有可能相关的.
公共类 MainActivity 扩展 Activity {
TextView timerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerText = (TextView) findViewById(R.id.timer_text);
}
@Override
protected void onStart() {
super.onStart();
CountDownTimer currentTimer = new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timerText.setText("" + (int) Math.ceil(millisUntilFinished / 1000.0));
}
@Override
public void onFinish() {
}
};
currentTimer.start();
}
Run Code Online (Sandbox Code Playgroud)
}
在4.1.2模拟器上测试并解决。
我的猜测是最终的参考变量导致了问题。
| 归档时间: |
|
| 查看次数: |
589 次 |
| 最近记录: |