gpt4 book ai didi

java - setText 不出现/更新

转载 作者:行者123 更新时间:2023-11-30 09:33:51 25 4
gpt4 key购买 nike

我的程序给用户一条指令,然后他们有 n 秒 时间做出响应。但是,当它要求您Do Nothing 时, 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/

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