如何动态更改 Android 选项卡文本?

发布于 2024-09-18 09:44:23 字数 1661 浏览 5 评论 0原文

这看起来应该很简单,但我想不出办法。我需要一个选项卡来包含开始文本,但在用户从列表中选择一个项目后该文本会发生更改。我知道如何更改选项卡背景和颜色

mTabHost.getChildAt(index).setBackgroundColor();

,但没有更改选项卡指示器的选项。我尝试过使用 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");
}

也不起作用,还有其他一些尝试。有什么想法吗?提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

乄_柒ぐ汐 2024-09-25 09:44:23

尝试这个对我有用的最简单的方法:

tabLayout.getTabAt(index).setText("TabName");

try this easiest way which works for me :

tabLayout.getTabAt(index).setText("TabName");
哽咽笑 2024-09-25 09:44:23

哇。好吧,这很痛苦。显然,TabWidget 使用relativelayout 做了一些奇怪的事情,每次我尝试使用radek-k 的解决方案做任何事情时,它都会因relativelayout 错误而崩溃。所以基本上解决方法如下。它还允许您更改字体、字体大小、字体颜色等

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

或在一行中...

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

其中“textIndex”是文本字段的索引。在本例中为 1。如果选项卡有图标或自定义修改,则索引可能会更改。

再次感谢 radek-k。你绝对给我指明了正确的方向。

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.

凌乱心跳 2024-09-25 09:44:23

您可以使用 findViewById 在不知道 TextView 的魔法索引的情况下完成此操作:

TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");

You can do it without knowing the magic index of the TextView by using findViewById:

TabWidget vTabs = getTabWidget();
View indicatorView = vTabs.getChildAt(tabIndex);
((TextView) indicatorView.findViewById(android.R.id.title)).setText("NewTabText");
雨的味道风的声音 2024-09-25 09:44:23

这是您的布局:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/ref_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_chpter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_verse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.google.android.material.tabs.TabLayout>

这是您的代码:

TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));

Here is your layout:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/ref_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_chpter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/ref_verse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.google.android.material.tabs.TabLayout>

Here is your code:

TabLayout tabLayout = findViewById(R.id.ref_tabs);
tabLayout.getTabAt(0).setText("BOOK"));
tabLayout.getTabAt(1).setText("CHAPTER"));
tabLayout.getTabAt(2).setText("VERSE"));
静水深流 2024-09-25 09:44:23
TabWidget vTabs = .....;

// get desired tab view  
View vTab = vTabs.getChildAt(i);

// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;

vText.setText(...);
TabWidget vTabs = .....;

// get desired tab view  
View vTab = vTabs.getChildAt(i);

// I guess vTab is instance of TextView
TextView vText = (TextView) vTab;

vText.setText(...);
岁月无声 2024-09-25 09:44:23

你可以这样做

TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");

,其中 android.R.id.title 是系统生成的,只需根据您的需要更改 ChildIndexText

You can do it like this

TabHost tabHost = getTabHost();
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv.setText("New Tab Text");

Where android.R.id.title is System generated, just change ChildIndex and Text according to your need

泅人 2024-09-25 09:44:23

这很简单。尝试一下。有用。

TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");

This is simple and easy. Try it. It works.

TabWidget tab = (TabWidget) findViewById (R.id.tab1);
((TextView)tab.getChildTabViewAt (1)).setText ("New name here");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文