动态改变textview里字体的颜色 Thread+Hander+Html.fromHtml

截图:




源代码:


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;

}

}

}; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值