TextView.setText()仅在sleep()后显示最终更改

时间:2015-08-27 11:22:10

标签: java android xml

我对Android比较陌生,细节通常会导致我的程序无法运行。 TextView(在本例中为tvQuestion)应该更改其内容,等待,然后再次更改它们,但是没有看到第一个更改,程序等待并仅显示最后一个更改。 旁注,所有内容都是从SQLite数据库中收集并放入自定义对象中的。 我认为问题出在onClick(View v)方法中,但是我们非常感谢所有的帮助。

这是我的代码:

package com.infernalpoison.falseorfact.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.infernalpoison.falseorfact.R;

public class Play extends Activity implements View.OnClickListener {
 final private Context playContext = this;
 private int correct = 0;
 private int wrong = 0;
 private TextView tvQuestion;
 private ImageView btnFalse, btnFact;
 private Question questionObj;
 private Thread thrNewQuestion = new Thread() {
    public void run() {
        DatabaseConstruction dbc = new DatabaseConstruction(playContext);
        try {
            dbc.open();
            questionObj = dbc.getRdmQ();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tvQuestion.setText(questionObj.getQuestion());
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);// Remove title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); // Remove notification bar

    setContentView(R.layout.activity_play);

    tvQuestion = (TextView) findViewById(R.id.tvQuestion);
    btnFact = (ImageView) findViewById(R.id.btn_fact);
    btnFact.setOnClickListener(this);
    btnFalse = (ImageView) findViewById(R.id.btn_false);
    btnFalse.setOnClickListener(this);

    questionObj = new Question();

    thrNewQuestion.run();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        **case R.id.btn_fact:
            factSelect();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thrNewQuestion.run();
            break;
        case R.id.btn_false:
            falseSelect();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thrNewQuestion.run();
            break;**
    }
}

private void factSelect() {
    if (questionObj.isAnswer() == true) {
        correct++;
        tvQuestion.setText("That is Correct! Well Done!");
    } else {
        wrong++;
        tvQuestion.setText("That is Incorrect. " + questionObj.getFactCorrection());
        //TODO ADD MECH TO EXIT
    }
}

private void falseSelect() {
    if (questionObj.isAnswer() == false) {
        correct++;
        tvQuestion.setText("That is Correct! Well Done!");
    } else {
        wrong++;
        tvQuestion.setText("That is Incorrect. " + questionObj.getFalseCorrection());
        //TODO ADD MECH TO EXIT
    }
}
}

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"
tools:context="com.infernalpoison.falseorfact.util.PlayFragment">

<ImageView

    android:id="@+id/btn_false"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:adjustViewBounds="true"
    android:clickable="true"
    android:scaleType="fitXY"
    android:src="@drawable/btn_false" />

<TextView
    android:id="@+id/tvQuestion"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:text="Empty" />

<ImageView

    android:id="@+id/btn_fact"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:adjustViewBounds="true"
    android:clickable="true"
    android:scaleType="centerCrop"
    android:src="@drawable/btn_fact" />

提前致谢。 PS这是我的第一篇文章,如果您需要更多信息,请通知我!

1 个答案:

答案 0 :(得分:1)

而不是:

  try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thrNewQuestion.run();

做这样的事情:

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            thrNewQuestion.run();
        }
    }, 2000);