gpt4 book ai didi

java - TextView.setText() 仅显示 sleep() 后的最终变化

转载 作者:行者123 更新时间:2023-11-30 01:55:55 26 4
gpt4 key购买 nike

我是 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 这是我的第一篇文章,所以如果您需要更多信息,请通知我!

最佳答案

代替:

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

做这样的事情:

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

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

关于java - TextView.setText() 仅显示 sleep() 后的最终变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com