java - setText 未出现/更新

标签 java android settext

我的程序向用户发出指令,然后他们有 n 秒 的时间来响应。但是,当它要求您不执行任何操作时, TextView 根本不会更新,其他指令会正常弹出,并且它们的编码都相同。

    public class SimpleActivity extends Activity implements SimpleGestureListener {

    String str = "";
    String strDirection = "";
    Random ranDirection = new Random();
    int intDirection;

    int scoreCounter;
    long timeStart;
    long timeEnd;

    TextView score;
    TextView allerts;
    TextView correct;

    private SimpleGestureFilter detector;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture);
        detector = new SimpleGestureFilter(this, this);

        scoreCounter = 0;
        catchValues();

    }

    public boolean dispatchTouchEvent(MotionEvent me) {
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
    }

    public void catchValues() {

        /** Linked to gesture.xml */

        score = (TextView) findViewById(R.id.tvScore);
        allerts = (TextView) findViewById(R.id.tvAllerts);
        correct = (TextView) findViewById(R.id.tvCorrect);

        initiateRandom();

    }

    public void initiateRandom() {

        /** Generate random move */

        /** Log start time of move */

        long start = System.currentTimeMillis();
        timeStart = start;

        intDirection = ranDirection.nextInt(5);

        if (intDirection == 0) {
            allerts.setText("Please Swipe Up!");
            randomColour();
        } else if (intDirection == 1) {
            allerts.setText("Please Swipe Down!");
            randomColour();
        } else if (intDirection == 2) {
            allerts.setText("Please Swipe Left!");
            randomColour();
        } else if (intDirection == 3) {
            allerts.setText("Please Swipe Right!");
            randomColour();
        } else if (intDirection == 4) {
            allerts.setText("Do Nothing!");
            randomColour();

            doNothing();

        }

    }

    public void onSwipe(int direction) {

        /** Detect swipe and store result */

        switch (direction) {

        case SimpleGestureFilter.SWIPE_RIGHT:
            strDirection = "right";

            break;
        case SimpleGestureFilter.SWIPE_LEFT:
            strDirection = "left";

            break;
        case SimpleGestureFilter.SWIPE_DOWN:
            strDirection = "down";

            break;
        case SimpleGestureFilter.SWIPE_UP:
            strDirection = "up";


            break;
        }

        checkSwipe();

    }

    public void checkSwipe() {

        /** Compare time from instruction to action */

        long end = System.currentTimeMillis();
        timeEnd = end;

        /** Verify the action was correct */

        if (timeEnd < timeStart + 2000) {

            if (intDirection == 0) {
                if (strDirection.contentEquals("up")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }

            } else if (intDirection == 1) {
                if (strDirection.contentEquals("down")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }
            } else if (intDirection == 2) {
                if (strDirection.contentEquals("left")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }
            } else if (intDirection == 3) {
                if (strDirection.contentEquals("right")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }

            }

        } else {

            /** Incorrect action, reset score and start over */

            scoreCounter = 0;
            score.setText("Score:" + scoreCounter);
            strDirection = "";
            correct.setText("Too Slow!");
            initiateRandom();

        }

    }

    @Override
    public void onDoubleTap() {
        // TODO Auto-generated method stub

    }

    public void correctSwipe() {
        scoreCounter = scoreCounter + 100;
        score.setText("Score:" + scoreCounter);
        correct.setText("Correct!");
        strDirection = "";
        initiateRandom();

    }

    public void wrongSwipe() {
        correct.setText("Wrong!");
        scoreCounter = 0;
        strDirection = "";
        score.setText("Score:" + scoreCounter);

    }

    public void randomColour() {

        Random txtColor = new Random();

        allerts.setTextColor(Color.rgb(txtColor.nextInt(255),
                txtColor.nextInt(255), txtColor.nextInt(255)));
    }

    public void doNothing() {

        /** Does nothing for n seconds */

        long end = System.currentTimeMillis();
        timeEnd = end;

        while (timeEnd < timeStart + 2000) {

            timeEnd = System.currentTimeMillis();
        }

        if (strDirection.contentEquals("up")
                || strDirection.contentEquals("down")
                || strDirection.contentEquals("left")
                || strDirection.contentEquals("right")) {
            wrongSwipe();
        } else {
            correctSwipe();
        }

    }

}

最佳答案

当您想从非 UI 线程更新 UI 时,请尝试使用 runOnUiThread(Runnable r):

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

类似于:

 Activity_Name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // here you put updates to the UI

        }
    });

或者使用onProgressUpdate() AsyncTask 的方法。

引用:

关于java - setText 未出现/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11994353/

相关文章:

java - setCellValueFactory 的正确用法

java - 为什么 AccessibilityManager.sInstance 会导致内存泄漏?

java - Enum 在 android/java 中与 .setText 一起使用

android - timer.setText (“setTextHere”)在线程内不起作用

java - 如何更改新 Material 主题中后退箭头的颜色?

android - setText方法导致Android App崩溃

java - Tomcat6 中的类路径资源(适用于 Jetty)

java - 为什么gradientdrawable.setBackground会让我的应用程序崩溃?

android - 从其他 View 的位置动画/平移 View

java - 应用程序显示与 Google 商店中的 Moto E 不兼容