2012-08-16 67 views
2

我的程序给了用户一条指令,然后他们有n seconds作出回应。但是,当它要求您Do Nothing文本视图根本不会更新,其他指令弹出正常,他们都编码相同。setText not appear/updating

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(); 
     } 

    } 

} 

回答

2

尝试使用runOnUiThread(Runnable r)当你想从一个非UI线程更新UI:

运行在UI线程上指定的动作。如果当前线程是 的UI线程,那么该动作立即执行。如果当前 线程不是UI线程,则该操作将被发布到UI线程的事件队列 中。

是这样的:

Activity_Name.this.runOnUiThread(new Runnable() { 

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

     } 
    }); 

可选择地使用的AsyncTaskonProgressUpdate()方法。

参考: