增加tab提示效果

本文介绍了一种在Android应用中使用TabActivity实现动态提示指定Tab页收到新数据的方法。通过自定义Tab并结合线程控制,实现了Tab页标题颜色的变化以提示用户有新的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说一下需求,我有多个tab页,每个tab也有新数据时我要动态提示是那个tab页收到了新数据。
我采用的继承TabActivity然后用
.addTab(myTabhost.newTabSpec("Sex")// make a new Tab
.setIndicator("首页",getResources().getDrawable(R.drawable.a_1))
// set the Title and Icon
.setContent(new Intent(this,HomeActivity.class)));
然后我采用的是线程来控制,改变tab页text的颜色
private TabHost myTabhost;
private final int START=1;
private Message msg;
private TextView tv;
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
Thread.sleep(500);
//不停切换
} catch (InterruptedException e) {
}
}
}
}).start();
private final Handler handler = new Handler() {

public void handleMessage(Message msg) {
super.handleMessage(msg);

switch (msg.what) {
case START:
TabHost host=getTabHost();
TabWidget widget=host.getTabWidget();
tv = (TextView) widget.getChildAt(2).findViewById(android.R.id.title);//这儿getChildAt(2)是选择的第三个tab页进行提示,这儿可以动态给值
String r=decToHex((int)Math.floor(Math.random()*256)-1);
String g=decToHex((int)Math.floor(Math.random()*256)-1);
String b=decToHex((int)Math.floor(Math.random()*256)-1);
tv.setTextColor(Color.parseColor("#"+r+g+b));
break;
}
}

};
public String decToHex(int dec)
{
String hexStr = "0123456789ABCDEF";
int low = Math.abs(dec % 16);
int high = Math.abs((dec - low)/16);
String hex = "" + (hexStr.length()>high?hexStr.charAt(high):hexStr.charAt(0)) + (hexStr.length()>low?hexStr.charAt(low):hexStr.charAt(10));
return hex;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值