0

I create simple Android app (https://www.linux.com/learn/docs/683628-android-programming-for-beginners-part-1) with latest Android Studio. Code:

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();
    }
}

My 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>

In emulator it's good working. But on my Galaxy S2 with CyanogenMod10.1(Android 4.2.2) app wrong updating TextView. Screenshot: enter image description here

How I can resolve this problem?

upd: after screen rotate TextView is updating once.

4

2 回答 2

0

当您将 UI 更新放在try块中、尝试避免它或使用 runOnUiThread 包装时,通常会发生这种情况。
编辑:

另一个原因-您将其更新为快速-您的代码每秒执行 1000 次更新,我认为它无法处理。

于 2013-07-17T14:26:57.553 回答
0

您可能想在每次更新时尝试使您的布局无效。我猜测文本更新的频率是手机没有足够的时间来重绘布局。这也可以解释为什么当您旋转手机时它会起作用,因为这样会强制更新布局。

countdownDisplay.invalidate();

如果这不起作用,请告诉我。

于 2013-07-17T14:23:21.177 回答