实现TextSwitcher

时间:2014-02-19 18:14:24

标签: android

我是Android应用程序开发的初学者。 我想创建一个卡拉OK风格的应用程序,不知何故,我可以在特定的时间间隔内更改图像。但是,我遇到了关于文本的问题,它不会随着歌曲的播放时间而改变。 我只是使用textview,但我想实现TextSwitcher,但我真的不知道如何。我知道如何使用ViewFlipper,但viewflipper仅用于静态间隔。 (我认为?) 请帮我显示文本,并在相应的时间内更改?谢谢。 还可以在不使用数组的情况下使用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,它没有更改。我不知道该怎么办了。 谢谢你的帮忙。 :)

1 个答案:

答案 0 :(得分:0)

当Handler稍后运行时,它会使用可用的sText的当前和最新内容。 你的所有处理程序都运行得很好,但是他们都看到了sText =的内容(“让它去/让她去混搭”+“Sam Tsui的”\ 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++;
    }
}; 

享受:)