2016-12-17 62 views
0

我想以5秒的间隔更改TextView的字符串。所以我写道:我想以5秒的间隔更改TextView的文本

Intent intent = getIntent(); 
    int kutisu = Integer.parseInt(intent.getStringExtra("kutisu"));// the number of times 
    int max = Integer.parseInt(intent.getStringExtra("max")); 
    int speed = 5000;// 5 seconds 

    int[] sum=new int[kutisu]; // After finished count, I want to ask for a total 
    int answer = 0; 
    for(int i=0;i<=kutisu;i++){ 
     sum[i]=new java.util.Random().nextInt(max); 
     tv.setText(sum[i]); //tv is a TextView 
     try { 
      Thread.sleep(speed); // stop 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

但是,字符串运行时不会改变。如何每5秒更改一次TextView的字符串?

回答

0

使用timerTask

public void startTimer() { 
//set a new Timer 
timer = new Timer(); 
//initialize the TimerTask's job --> change yourView text data 
initializeTimerTask(); 
//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms 
timer.schedule(timerTask, 5000, 10000); // 
} 

可以使用postDelayed过,以保持其在5秒的间隔连续运行,则需要再次调用postDelayed嵌套运行中的方法

handler.postDelayed(new Runnable() { 

    public void run() { 
    Log.d("MyActivity", "Ding Ding"); 
    // --> change yourView text data 
    //calling postdelayed again 
    handler.postDelayed(this, 5000); //added this line 
    } 
}, 5000); 
0
Handler handler = new Handler(); 
    private Runnable myRunnable=new Runnable(){ 
    public void run(){ 
     //Do what you want to your textView here 
     handler.postDelayed(this, 5000);  
    } 
}; 
1

复制以下类别

public class UIUpdater { 
    // Create a Handler that uses the Main Looper to run in 
    private Handler mHandler = new Handler(Looper.getMainLooper()); 

    private Runnable mStatusChecker; 
    private int UPDATE_INTERVAL = 5*1000; //5 seconds 

    /** 
    * Creates an UIUpdater object, that can be used to 
    * perform UIUpdates on a specified time interval. 
    * 
    * @param uiUpdater A runnable containing the update routine. 
    */ 
    public UIUpdater(final Runnable uiUpdater) { 
     mStatusChecker = new Runnable() { 
      @Override 
      public void run() { 
       // Run the passed runnable 
       uiUpdater.run(); 
       // Re-run it after the update interval 
       mHandler.postDelayed(this, UPDATE_INTERVAL); 
      } 
     }; 
    } 

    /** 
    * The same as the default constructor, but specifying the 
    * intended update interval. 
    * 
    * @param uiUpdater A runnable containing the update routine. 
    * @param interval The interval over which the routine 
    *     should run (milliseconds). 
    */ 
    public UIUpdater(Runnable uiUpdater, int interval){ 

     this(uiUpdater); 
     UPDATE_INTERVAL = interval; 
    } 

    /** 
    * Starts the periodical update routine (mStatusChecker 
    * adds the callback to the handler). 
    */ 
    public synchronized void startUpdates(){ 
     mStatusChecker.run(); 
    } 

    /** 
    * Stops the periodical update routine from running, 
    * by removing the callback. 
    */ 
    public synchronized void stopUpdates(){ 
     mHandler.removeCallbacks(mStatusChecker); 
    } 
} 

请注意,在上面的类变量UPDATE_INTERVAL中决定更新间隔,根据需要更改它。

,然后在活动或片段

UIUpdater mUIUpdater = new UIUpdater(new Runnable() { 
    @Override 
    public void run() { 
     //do what ever you want to do with your textview 
    } 
}); 

onResume

@Override 
public void onResume() { 
    super.onResume(); 
    mUIUpdater.startUpdates(); 
} 

而且在

@Override 
public void onPause() { 
    super.onPause(); 
    mUIUpdater.stopUpdates(); 

} 
0

您可以在同一个地方使用2 TextView的

private void startFirstFadeOut() { 
     Animation animation = new AlphaAnimation(1f, 0f); 
     animation.setDuration(ANIMATION_DURATION); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       firstTextView.setVisibility(INVISIBLE); 
       startSecondFadeIn(); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     firstTextView.setAnimation(animation); 
     firstTextView.animate(); 
    } 

    private void startSecondFadeIn() { 
     Animation animation = new AlphaAnimation(0f, 1f); 
     animation.setDuration(ANIMATION_DURATION); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       secondTextView.setVisibility(VISIBLE); 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       startSecondFadingOut(); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     secondTextView.setAnimation(animation); 
     secondTextView.animate(); 
    } 

    private void startSecondFadingOut() { 
     Animation animation = new AlphaAnimation(1f, 0f); 
     animation.setDuration(ANIMATION_DURATION); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       secondTextView.setVisibility(INVISIBLE); 
       startFirstFadingIn(); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     secondTextView.setAnimation(animation); 
     secondTextView.animate(); 
    } 

    private void startFirstFadingIn() { 
     Animation animation = new AlphaAnimation(0f, 1f); 
     animation.setDuration(ANIMATION_DURATION); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       firstTextView.setVisibility(VISIBLE); 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       startFirstFadeOut(); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     firstTextView.setAnimation(animation); 
     firstTextView.animate(); 
    } 

只需拨打startFirstFadeOut即可自动工作