截图:
源代码:
package com.example.fhf;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.Html;
import android.text.format.DateFormat;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private static int i=1;
private TextView textView1;
private String ct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1= (TextView) findViewById(R.id.text);
new TimeThread().start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class TimeThread extends Thread {
@Override
public void run() {
do {
try {
Thread.sleep(500);
Message msg = new Message();
msg.what = i;
i++;
mHandler.sendMessage(msg);
while(i>4)
{
i=1;
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
} while (true);
}
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 1:
ct="<font color=\"red\">有你</font><font color=\"blue\">朋友</font><font color=\"yellow\">遍</font><font color=\"green\">天下</font>";
textView1.setText(Html.fromHtml(ct));
break;
case 2:
ct="<font color=\"green\">有你</font><font color=\"red\">朋友</font><font color=\"blue\">遍</font><font color=\"yellow\">天下</font>";
textView1.setText(Html.fromHtml(ct));
break;
case 3:
ct="<font color=\"yellow\">有你</font><font color=\"green\">朋友</font><font color=\"red\">遍</font><font color=\"blue\">天下</font>";
textView1.setText(Html.fromHtml(ct));
break;
case 4:
String ct="<font color=\"blue\">有你</font><font color=\"yellow\">朋友</font><font color=\"grren\">遍</font><font color=\"red\">天下</font>";
textView1.setText(Html.fromHtml(ct));
break;
default:
break;
}
}
};
}