2014-02-19 41 views
1

我是Android應用程序開發的初學者。 我想製作一個卡拉OK風格的應用程序,不知何故,我可以在特定的時間間隔內改變圖像。但是,我對文本有問題,它不會隨着歌曲的播放時間而改變。 我只是使用textview,但我想實現TextSwitcher,但我真的不知道如何。我知道如何使用ViewFlipper,但viewflipper僅適用於靜態間隔。 (我認爲?) 請幫助我顯示文本,並使其與相應的時間改變?謝謝。 也可以使用TextSwitcher而不使用數組?我需要展示的文本太多,不能在我想的數組中。實現TextSwitcher

這裏是我到目前爲止有:

public class LetItGo extends Activity 
{ 
private ViewFlipper myviewflipper; 
TextView t; private String sText; 

private Handler mHandler = new Handler(); 

private Runnable mWaitRunnable = new Runnable() { 
    public void run() { 
     t.setText(sText); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sample); 
    myviewflipper = (ViewFlipper) findViewById(R.id.flipper1); 
     myviewflipper.setAutoStart(true); 
     myviewflipper.setFlipInterval(5000); 
     myviewflipper.startFlipping(); 


     t=(TextView)findViewById(R.id.textView1); 

     sText = ("Let It Go/Let Her Go Mash Up" + "\n by Sam Tsui"); 
     t.setText(sText);  

//1 
     sText = ("Let it go, let it go"); 
     mHandler.postDelayed(mWaitRunnable, 1000*10); 

//2   
     sText = ("You only need the light when its burning low"); 
     mHandler.postDelayed(mWaitRunnable, 6000*10*10); 

//3 
     sText = ("Let it go, let it go");   
     mHandler.postDelayed(mWaitRunnable, 9000*10); 

//4 
     sText = ("You only miss the sun when it starts to snow");   
     mHandler.postDelayed(mWaitRunnable, 13000*10); 

//5 
     sText = ("And here I stand, \n and here I'll stay"); 
     mHandler.postDelayed(mWaitRunnable, 17000*10); 

//6   
     sText = ("You only know you love her when you let her go"); 
     mHandler.postDelayed(mWaitRunnable, 19000*10); 

//7   
     sText = "The cold never bothered me anyway"; 
     mHandler.postDelayed(mWaitRunnable, 25000*10);  

//8   
     sText = ("Staring at the bottom of your glass"); 
     mHandler.postDelayed(mWaitRunnable, 34000*10); 

//9   

     sText = ("Hoping one day you'll make a dream last "); 
     mHandler.postDelayed(mWaitRunnable, 37000*10); 

//10 
     sText = ("But dreams come slow"); 
     mHandler.postDelayed(mWaitRunnable, 4000*100); 

//11    
     sText = ("and they go so fast"); 
     mHandler.postDelayed(mWaitRunnable, 42000*10); 

//12   
     sText = ("You see her when you close your eyes "); 
     mHandler.postDelayed(mWaitRunnable, 47000*10); 

//13    
     sText = ("Maybe one day you'll understand why"); 
     mHandler.postDelayed(mWaitRunnable, 5000*100); 

//14   
     sText = "Everything you touch surely dies"; 
     mHandler.postDelayed(mWaitRunnable, 54000*10); 

//15   
     sText = ("Don’t let them in, don’t let them see "); 
     mHandler.postDelayed(mWaitRunnable, 62000*10); 

//16    
     sText = ("Be the good guy you always have to be "); 
     mHandler.postDelayed(mWaitRunnable, 65000*10); 

//17    
     sText = ("Conceal, don’t feel, don’t let them know "); 
     mHandler.postDelayed(mWaitRunnable, 69000*10); 

//18    
     sText = ("Well, now they know"); 
     mHandler.postDelayed(mWaitRunnable, 75000*10); 

//19    
     sText = ("Let it go, let it go"); 
     mHandler.postDelayed(mWaitRunnable, 78000*10); 

//20    
     sText = ("You only need the light when its burning low"); 
     mHandler.postDelayed(mWaitRunnable, 81000*10); 

//21 
     sText = ("Let it go, let it go");   
     mHandler.postDelayed(mWaitRunnable, 85000*10); 

//22 
     sText = ("You only miss the sun when it starts to snow");   
     mHandler.postDelayed(mWaitRunnable, 88000*10);  

//23 
     sText = ("And here I stand, \n and here I'll stay"); 
     mHandler.postDelayed(mWaitRunnable, 92000*10);  

//24    
     sText = ("You only know you love her when you let her go"); 
     mHandler.postDelayed(mWaitRunnable, 98000*10); 

//25    
     sText = ("The cold never bothered me anyway"); 
     mHandler.postDelayed(mWaitRunnable, 104000*10); 

//26    
     sText = ("Let it go,"); 
     mHandler.postDelayed(mWaitRunnable, 112000*10); 

//27    
     sText = ("let her go"); 
     mHandler.postDelayed(mWaitRunnable, 116000*10); 

//28    
     sText = ("You only need the light when it's burning low"); 
     mHandler.postDelayed(mWaitRunnable, 121000*10); 

//29    
     sText = ("You only miss the sun when it starts to snow"); 
     mHandler.postDelayed(mWaitRunnable, 124000*10); 

//30      
     sText = ("You only know you love her when you let her go"); 
     mHandler.postDelayed(mWaitRunnable, 127000*10); 

//31    
     sText = ("You only know you've been high when you're feeling low"); 
     mHandler.postDelayed(mWaitRunnable, 134000*10); 

//32    
     sText = ("You only hate the road when you’re missin' home"); 
     mHandler.postDelayed(mWaitRunnable, 138000*10); 

//33    
     sText = ("You only know you love her when you let her go"); 
     mHandler.postDelayed(mWaitRunnable, 141000*10); 

//34 
     sText = ("And now you know ooooooh"); 
     mHandler.postDelayed(mWaitRunnable, 146000*10); 

//35 
     sText = ("Let it go, let it go"); 
     mHandler.postDelayed(mWaitRunnable, 152000*10); 

//36    
     sText = ("You only need the light when its burning low"); 
     mHandler.postDelayed(mWaitRunnable, 155000*10); 

//37 
     sText = ("Let it go, let it go");   
     mHandler.postDelayed(mWaitRunnable, 158000*10); 

//38 
     sText = ("You only miss the sun when it starts to snow");   
     mHandler.postDelayed(mWaitRunnable, 162000*10); 

//39 
     sText = ("And here I stand,"); 
     mHandler.postDelayed(mWaitRunnable, 166000*10); 


//40    
     sText = ("and here I'll stay"); 
     mHandler.postDelayed(mWaitRunnable, 169000*10); 


//41    
     sText = ("You only know you love her when you let her go" + " \n oooooohhhhh..."); 
     mHandler.postDelayed(mWaitRunnable, 172000*10); 

//42    
     sText = ("dudududu... "); 
     mHandler.postDelayed(mWaitRunnable, 181000*10); 

//43    
     sText = ("The cold never bothered me anyway"); 
     mHandler.postDelayed(mWaitRunnable, 192000*10); 

//44 
     sText = ("Let It Go/Let Her Go Mash Up" + "\n by Sam Tsui"); 
     mHandler.postDelayed(mWaitRunnable, 197000*10); 


} 

} 

我不知道如果我這樣做是正確的實際。根據我的研究,我把這些東西放在一起。 在這段代碼中,顯示了最後一個sText,它沒有改變。我不知道該做什麼了。 感謝您的幫助。 :)

+0

告訴我你得到什麼樣的行爲和什麼你想要嗎? – Behnam

+0

@Campiador至於圖片是的。但對於文本沒有。顯示的唯一文本是最後一集,它是註釋44.它不會改變。 – user3329377

+0

@ user3329337:看到我​​的回答,讓我知道它是否解決了您的問題。 – Behnam

回答

0

Handler運行時,它使用當前和最新的可用sText內容。 你的所有處理程序都很好用,但他們都看到了sText =(「讓它去吧/讓她去吧」+ Sam Soli的「\ n」),這是最後一個sText =(「sometext」)調用; (第44個) ,因爲sText =(「something」)命令全部在應用程序運行時的第一時刻執行。

那該怎麼辦?

除去所有

sText = ("Some Text"); 

除了第一個(歌曲標題)。

重寫mWaitRunnable就象這樣:

聲明一個整數你要打印的行號:

int lineNumber = 1; 

private Runnable mWaitRunnable = new Runnable() { 
    public void run() { 

     switch (lineNumber) { 


     case 1: 
     sText = ("Let it go, let it go"); 
      break; 
     case 2: 

     sText = ("You only need the light when its burning low"); 
      break; 
     case 3: 
     sText = ("Let it go, let it go");   
      break; 

     case 4: 
      sText = ("You only miss the sun when it starts to snow");     
       break; 

// continue up to case 44: 

      default: 
       break; 
      } 

      t.setText(sText); 
      lineNumber++; 
    } 
}; 

享受:)

+0

@ user3329337:歡迎,您可能想提高我的答案;) – Behnam