2015-01-20 5 views
0

내 탭에 리스너를 추가하는 방법. 그래서 내가하고 싶은 일은 탭을 클릭 할 때 뭔가하고 싶다는 것입니다. 이 탭은 pressed.Please가 나를 도울 때 결론에내 탭에 리스너 추가

<?xml version="1.0" encoding="utf-8"?> 
<TabHost android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/tabHost" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<TabWidget 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@android:id/tabs" 
    /> 
<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@android:id/tabcontent" 
    > 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tab1" 
     android:orientation="vertical" 
     android:paddingTop="60px" 
     > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="100px" 
      android:text="This is tab1" 
      android:id="@+id/txt1" 
      /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/tab2" 
     android:orientation="vertical" 
     android:paddingTop="60px" 
     > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="100px" 
      android:text="This is tab 2" 
      android:id="@+id/txt2" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/tab3" 
     android:orientation="vertical" 
     android:paddingTop="60px" 
     > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="100px" 
      android:text="This is tab 3" 
      android:id="@+id/txt3" 
      /> 

    </LinearLayout> 
</FrameLayout> 

그래서 내가 뭔가를하고 싶은 : 여기

package app.my.com.wave; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TabHost; 


public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    TabHost tabHost = (TabHost) findViewById(R.id.tabHost); 
    tabHost.setup(); 

    TabHost.TabSpec spec1 = tabHost.newTabSpec("Tab 1"); 
    spec1.setContent(R.id.tab1); 
    spec1.setIndicator("Tab 1"); 

    TabHost.TabSpec spec2 = tabHost.newTabSpec("Tab 2"); 
    spec2.setIndicator("Tab 2"); 
    spec2.setContent(R.id.tab2); 

    TabHost.TabSpec spec3 = tabHost.newTabSpec("Tab 3"); 
    spec3.setIndicator("Tab 3"); 
    spec3.setContent(R.id.tab3); 

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2); 
    tabHost.addTab(spec3); 
}} 

내 XML 폴더입니다 : 다음은 탭의 코드입니다 그리고 미리 감사드립니다. 당신은 콜백을 등록 할 수 있습니다

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(String arg0) { 
    Log.e("Tab number ", "" + tabHost.getCurrentTab()); 
    }  

답변

1

사용 setOnTabChangeListener이 호출 할 때 탭의 변경이 목록에있는 모든 항목의 선택 상태입니다. tabwidget의 특정보기를 클릭하면서 콜백을 추가 W/호출 할 f를

mTabHost.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(String arg0) { 
    Log.d("MainActivity", "current tab index :: " + mTabHost.getCurrentTab()); 
    }  

것은 tabhost 안드로이드를 설정하는 동안. 변경된 탭 인덱스가있는 setCurrenttab 메소드 다음에 onTabChange 호출이 차례로 호출됩니다.

0