如何动态更改Android的标签文字? [英] How to change android tab text on the fly?

查看:253
本文介绍了如何动态更改Android的标签文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该是简单的,但我不能想出一个办法做到这一点。我需要一个标签,开始有文字,但后来有文字更改后用户从列表中选择一个项目。我知道如何通过

更改标签背景和颜色

  mTab​​Host.getChildAt(指数).setBackgroundColor();
 

但没有一个选项来改变标签的指示。我用一个EditText尝试。

 私人的EditText TABNAME;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.statistics);

    通讯=新通讯();

    TABNAME =新的EditText(本);
    tabName.setText(BeginningText);

    mTabHost = getTabHost();
    mTabHost.addTab(mTabHost
                   .newTabSpec(tab_1_stat)
                   .setIndicator(用户)
                   .setContent(R.id.meStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec(tab_2_stat)
                   .setIndicator(tabName.getText())
                   .setContent(R.id.themStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec(tab_3_stat)
                   .setIndicator(档案)
                   .setContent(R.id.archiveStatsTab));
    mTabHost.setOnTabChangedListener(本);
    。getTabWidget()getChildAt(1).setOnClickListener(新onThemTabClicked());
    mTabHost.setCurrentTab(0);

    onTabChanged(tab_1_stat);
 

}

......

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图意图){
    super.onActivityResult(要求code,因此code,意图);

    tabName.setText(ChangedTabText);
    捆绑额外= intent.getExtras();
    themStats = extras.getStringArray(themStats);
    mTabHost.setCurrentTab(1);
    onTabChanged(tab_2_stat);
}
 

这是没有任何工作,再加上一些其他的尝试。有任何想法吗?由于时间提前!

解决方案

哇。好吧,这是一个痛苦。显然TabWidget做一些时髦与RelativeLayout的,每次我试图做拉狄克-K的解决方案,它被吹了一个RelativeLayout的错误东西。所以基本上解决办法是如下。它还允许你改变字体,字体大小,字体颜色等。

  TabWidget vTabs = getTabWidget();
RelativeLayout的rLayout =(RelativeLayout的)vTabs.getChildAt(的tabIndex);
((TextView中)rLayout.getChildAt(textIndex))的setText(NewTabText)。
 

或在同一行...

<$p$p><$c$c>((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");

,其中textIndex是文本字段的索引。在这种情况下,它是1,如果选项卡具有一个图标或定制修改所述索引可以改变

再次

由于拉狄克-K。你一定让我指出了正确的方向。

This seems like it should be simple, but I can't figure out a way to do it. I'm needing a tab to have beginning text, but then have that text change after the user selects an item from a list. I know how to change tab backgrounds and colors via

mTabHost.getChildAt(index).setBackgroundColor();

but there isn't an option to change the tab's indicator. I've tried using an EditText.

private EditText tabName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistics);

    comm = new Communicator();

    tabName = new EditText(this);
    tabName.setText("BeginningText");

    mTabHost = getTabHost();
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_1_stat")
                   .setIndicator(User)
                   .setContent(R.id.meStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_2_stat")
                   .setIndicator(tabName.getText())
                   .setContent(R.id.themStatsTab));
    mTabHost.addTab(mTabHost
                   .newTabSpec("tab_3_stat")
                   .setIndicator("Archive")
                   .setContent(R.id.archiveStatsTab));
    mTabHost.setOnTabChangedListener(this);
    getTabWidget().getChildAt(1).setOnClickListener(new onThemTabClicked());
    mTabHost.setCurrentTab(0);

    onTabChanged("tab_1_stat");

}

.....

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    tabName.setText("ChangedTabText");
    Bundle extras = intent.getExtras();
    themStats = extras.getStringArray("themStats");
    mTabHost.setCurrentTab(1);
    onTabChanged("tab_2_stat");
}

That didn't work either, along with a few other attempts. Any ideas? Thanks ahead of time!

解决方案

Wow. Okay this was a pain. Apparently TabWidget does something funky with RelativeLayout and everytime I tried to do anything with radek-k's solution it was blowing up with a RelativeLayout error. So basically the work around is the following. It also allows you to change the font, font size, font color, etc.

TabWidget vTabs = getTabWidget();
RelativeLayout rLayout = (RelativeLayout) vTabs.getChildAt(tabIndex);
((TextView) rLayout.getChildAt(textIndex)).setText("NewTabText");

or in one line...

((TextView)((RelativeLayout)getTabWidget().getChildAt(tabIndex)).getChildAt(textIndex)).setText("NewTabText");

where "textIndex" is the index of the text field. In this case it is 1. If the tab has an icon or custom modifications the index could change.

Thanks again to radek-k. You definitely got me pointed in the right direction.

这篇关于如何动态更改Android的标签文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆