我的程序为用户提供说明,然后他们有n seconds
回复。但是,当它要求您Do Nothing
文本视图根本没有更新时,其他说明会弹出正常,并且它们的编码都相同。
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();
}
}
}
答案 0 :(得分:2)
如果要从非UI线程更新UI,请尝试使用runOnUiThread(Runnable r)
:
在UI线程上运行指定的操作。如果当前线程是 UI线程,然后立即执行操作。如果是当前的 线程不是UI线程,操作被发布到事件队列 UI线程。
类似的东西:
Activity_Name.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// here you put updates to the UI
}
});
或者使用AsyncTask
的{{3}}方法。
<强>参考:强>