如何在android中使用OnTabChanged方法?

时间:2012-06-12 12:48:31

标签: android android-tabhost android-tabactivity

       TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, NewUserActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();

    spec = tabHost.newTabSpec("New User").setIndicator("New User").setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, ExistingUserActivity.class);
    spec = tabHost.newTabSpec("Existing User").setIndicator("Existing User").setContent(intent);
    tabHost.addTab(spec);

    tabHost.getTabWidget()
            .getChildAt(0)
            .setLayoutParams(
                    new LinearLayout.LayoutParams((width / 2) - 2, 40));
    tabHost.getTabWidget()
            .getChildAt(1)
            .setLayoutParams(
                    new LinearLayout.LayoutParams((width / 2) - 2, 40));
    TabWidget tw = getTabWidget();

    tw.getChildAt(0).setBackgroundColor(Color.parseColor("#800000"));
    tw.getChildAt(1).setBackgroundColor(Color.parseColor("#FF6347"));

}

@Override
public void onTabChanged(String x) {

}

上面的代码初始化了TabHost。它使用两个选项卡。一个新用户和另一个现有用户。点击它们中的任何一个,我希望标签被聚焦并且颜色与另一个不同。我不知道如何使用OnTabChanged方法。

2 个答案:

答案 0 :(得分:4)

  

单击其中任何一个,我希望选项卡是焦点和   与另一种颜色不同。

您可以按照以下方式实施。

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            public void onTabChanged(String str) {
                tabHost.getCurrentTabView().setBackgroundColor(color);
                tabHost.getCurrentTabView().setBackgroundDrawable(drawable);
                tabHost.getCurrentTabView().setBackgroundResource(resid);
            }
        });

答案 1 :(得分:0)

要设置具有不同状态的选项卡,请创建以下两种方法:

private void setupTab(final View view, final String tag, Intent intent) {
    View tabview = createTabView(tabHost.getContext(), tag);
    TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
    tabHost.addTab(tabSpec);
}

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);
    tv.setTypeface(tf);
    return view;
}

然后在布局文件夹中创建tabs_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
   <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:text="Title"
      android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>

现在在drawable文件夹中,创建xml文件。 tab_bg_selector.xml,tab_bg_selected.xml和tab_bg_unselected.xml:

--- TAB SELECTOR ---

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  Active tab -->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!--  Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!--  Pressed tab -->
<item android:state_pressed="true" android:drawable="@drawable/tab_bg_selected" />
<!--  Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
</selector>

--- TAB SELECED ---

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
    android:endColor="#696969" android:angle="-90" />
</shape>

--- TAB UNSELECTED ---

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
    android:endColor="#222222" android:angle="-90" />
</shape>

将颜色设置为您想要的颜色,祝您好运! 哦,我差点忘了,在tabhost中设置Intent时,请使用setupTab方法,如下所示:

    Intent i = new Intent(this, AAACalendarActivity.class);
    setupTab(new TextView(this), "Month", i);
    i = new Intent(this, AAACalendarWeekActivity.class);
    setupTab(new TextView(this), "Week", i);
    i = new Intent(this, AAACalendarDayActivity.class);
    setupTab(new TextView(this), "Day", i);