在 Android 中向 TabActivity 添加意图

2024-04-12

我查了一些关于如何在android中构建TAB的在线教程,并看到了符合我要求的教程。我很高兴实施它并且效果很好。当我希望每个单独的选项卡推送到一个单独的活动时,问题就出现了。我无法开始并打算传递控制权。我只给出了关于如何制作选项卡的主要代码,因为构建所需的 xml 在这个问题中没有任何功能。

这是代码:

public class Secondactivity extends TabActivity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setupTabHost();
    mTabHost.getTabWidget().setDividerDrawable(se.copernicus.activity.R.drawable.tab_divider);

    setupTab(new TextView(this), "Month");
    setupTab(new TextView(this), "Week");
    setupTab(new TextView(this), "Day");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);

TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
        .setContent(new TabContentFactory() {
            public View createTabContent(String tag) {
                return view;
            }
        });
mTabHost.addTab(setContent);

}

private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
        .inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
   }
}

我怎样才能开始一个新的意图,以便当单击选项卡时它应该从Secondactivity to WeekActivity or DayActivity ?


public class MainTabActivity extends TabActivity

{
    private TabHost mTabHost;

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

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    setupTab(new TextView(this), getString(R.string.live));
    setupTab(new TextView(this), getString(R.string.guide));
    setupTab(new TextView(this), getString(R.string.remotes));
    setupTab(new TextView(this), getString(R.string.settings));

    mTabHost.setCurrentTabByTag(getString(R.string.live));
}

private void setupTab(final View view, final String tag)
{
    View tabview = createTabView(mTabHost.getContext(), tag);

    if (tag.compareTo(getString(R.string.live)) == 0)
    {
        Intent intent = new Intent(getApplicationContext(), LiveActivity.class);

        TabSpec setContent = mTabHost.newTabSpec(getString(R.string.live)).setIndicator(tabview).setContent(new TabHost.TabContentFactory()
        {

            public View createTabContent(String tag)
            {
                return view;
            }

        });

        setContent.setContent(intent);
        mTabHost.addTab(setContent);
    }

    if (tag.compareTo(getString(R.string.guide)) == 0)
    {
        Intent intent = new Intent(getApplicationContext(), ProgramGuide.class);

        TabSpec setContent = mTabHost.newTabSpec(getString(R.string.guide)).setIndicator(tabview).setContent(new TabHost.TabContentFactory()
        {

            public View createTabContent(String tag)
            {
                return view;
            }

        });

        setContent.setContent(intent);
        mTabHost.addTab(setContent);
    }

    if (tag.compareTo(getString(R.string.remotes)) == 0)
    {
        Intent intent = new Intent(getApplicationContext(), RemoteMultiPanel.class);

        TabSpec setContent = mTabHost.newTabSpec(getString(R.string.remotes)).setIndicator(tabview).setContent(new TabHost.TabContentFactory()
        {

            public View createTabContent(String tag)
            {
                return view;
            }

        });

        setContent.setContent(intent);
        mTabHost.addTab(setContent);
    }

    if (tag.compareTo(getString(R.string.settings)) == 0)
    {
        Intent intent = new Intent(getApplicationContext(), SettingsMain.class);

        TabSpec setContent = mTabHost.newTabSpec(getString(R.string.settings)).setIndicator(tabview).setContent(new TabHost.TabContentFactory()
        {

            public View createTabContent(String tag)
            {
                return view;
            }

        });

        setContent.setContent(intent);
        mTabHost.addTab(setContent);
    }
}

private static View createTabView(final Context context, final String text)
{
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);

    int resId = 0;
    ImageView iconImageView = (ImageView) view.findViewById(R.id.imageView1);

    if (text.compareTo(context.getString(R.string.settings)) == 0)
    {
        resId = R.drawable.settings_icon;
    }
    else if (text.compareTo(context.getString(R.string.remotes)) == 0)
    {
        resId = R.drawable.remotes_icon;
    }
    else if (text.compareTo(context.getString(R.string.live)) == 0)
    {
        resId = R.drawable.live_icon;
    }
    else if (text.compareTo(context.getString(R.string.guide)) == 0)
    {
        resId = R.drawable.guide_icon;
    }

    iconImageView.setImageResource(resId);

    return view;
}

}

查看 setupTap() 方法: 它控制引发活动的 4 种可能的意图: 现场活动、节目指南、...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android 中向 TabActivity 添加意图 的相关文章

随机推荐