如何使用OnTabChangeListener?

时间:2011-11-28 03:24:58

标签: java android android-layout android-widget

我有一个带有3个标签的android程序。当我点击“地图选项卡”时,我希望它显示“地图被选中”消息,如下图所示。但是,如果我单击“名称”选项卡,然后第二次返回“地图选项卡”,则“再次选择地图”消息将不会显示在屏幕上。每次选择该选项卡时,如何使“地图”选项卡显示“再次选择地图”消息?您可以编辑以下代码并显示有关如何使用OnTabChangeListener的示例吗?

public class MapsActivity extends MapActivity implements OnTabChangeListener {    

@Override
public void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main4);
    Toast.makeText(getApplicationContext(),"Map is Selected", Toast.LENGTH_LONG).show();        
}

public void onTabChanged(String tabId) {
    Toast.makeText(getApplicationContext(),"Map Selected Again", Toast.LENGTH_LONG).show(); 
}

@Override
protected boolean isRouteDisplayed() {
    return false;
} }

enter image description here

更新。但以下方法不输出消息。

`public class HelloTabWidget extends TabActivity implements OnTabChangeListener{`

    private TabHost mTabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); 
        TabHost tabHost = getTabHost();  
        TabHost.TabSpec spec;  
        Intent intent; 

        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CityActivity.class);
        spec = tabHost.newTabSpec("city").setIndicator("City",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent); 
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MapsActivity.class);
        spec = tabHost.newTabSpec("Map").setIndicator("Map",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
        tabHost.addTab(spec);        

        tabHost.setCurrentTab(2);
    }

    public void onTabChanged(String tabId) {
        mTabHost = getTabHost();
        mTabHost.setOnTabChangedListener(this);
        Toast.makeText(getApplicationContext(), "To check for Display", Toast.LENGTH_LONG).show();
        Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());      
    }} 

1 个答案:

答案 0 :(得分:3)

您应该将OnTabChangeListener实现到TabActivity类,而不是Tab的内容。

在TabActivity中实现OnTabChangeListener

然后为TabHost mTabHost.setOnTabChangedListener(this);

设置监听器
@Override
    public void onTabChanged(String tabId) {
        Log.i("selected tab ", tabId);

    }

<强>更新

public class HelloTabWidget extends TabActivity implements OnTabChangeListener{`

    private TabHost mTabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); 
        TabHost tabHost = getTabHost();  
        TabHost.TabSpec spec;  
        Intent intent; 
        mTabHost = getTabHost();


        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CityActivity.class);
        spec = tabHost.newTabSpec("city").setIndicator("City",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent); 
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MapsActivity.class);
        spec = tabHost.newTabSpec("Map").setIndicator("Map",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
        tabHost.addTab(spec);        

        tabHost.setCurrentTab(2);
        mTabHost.setOnTabChangedListener(this);
    }

    public void onTabChanged(String tabId) {
        Toast.makeText(getApplicationContext(), "Selected Tab "+tabId, Toast.LENGTH_LONG).show();
        Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());      
    }}