简体   繁体   中英

Wrong updating TextView in Android app

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: 在此处输入图片说明

How I can resolve this problem?

upd: after screen rotate TextView is updating once.

You might want to try invalidating your layout every time it is updated. I am guessing with how often the text is being updated the phone is not having enough time to redraw the layout. This would also explain why it works when you rotate your phone, because then the layout is forced to update.

countdownDisplay.invalidate();

Let me know if that does not work.

It commonly happens when you put UI updates inside try blocks, try to avoid it or wrap with runOnUiThread.
EDIT:

Another reason - you update it to fast - you code does 1000 updates per second i dont think it can handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM