我使用最新的Android Studio创建简单的Android应用程序(https://www.linux.com/learn/docs/683628-android-programming-for-beginners-part-1)。代码:在Android应用程序中更新TextView错误
public class test_act extends Activity {
private static final int MILLIS_PER_SECOND = 1000;
private static final int SECONDS_TO_COUNTDOWN = 30;
private android.widget.TextView countdownDisplay;
private android.os.CountDownTimer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_act);
countdownDisplay = (android.widget.TextView) findViewById(R.id.time_display_box);
android.widget.Button startButton = (android.widget.Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try {
showTimer(SECONDS_TO_COUNTDOWN * MILLIS_PER_SECOND);
} catch (NumberFormatException e) {
// method ignores invalid (non-integer) input and waits
// for something it can use
}
}
});
}
private void showTimer(int countdownMillis) {
if(timer != null) { timer.cancel(); }
timer = new android.os.CountDownTimer(countdownMillis, MILLIS_PER_SECOND) {
@Override
public void onTick(long millisUntilFinished) {
countdownDisplay.setText("counting down: " +
millisUntilFinished/MILLIS_PER_SECOND);
}
@Override
public void onFinish() {
countdownDisplay.setText("KABOOM!");
}
}.start();
}
}
我的XML:
<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" >
<TextView
android:id="@+id/time_display_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="@string/_00_30"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/startbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time_display_box"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:text="@string/start" />
</RelativeLayout>
在模拟器这是很好的工作。但在我的银河S2与CyanogenMod10.1(Android 4.2.2)应用程序错误更新TextView。屏幕截图:
我该如何解决这个问题?
upd:屏幕旋转后TextView正在更新一次。
你可以添加一张图片,看看它应该是什么样子并发布你的xml文件? – buczek
buczek,在模拟器中:http://s017.radikal.ru/i415/1307/3a/cfddc768498a.png – pkot
我的XML我添加到问题。 – pkot