How to use OnTabChangeListener?

2019-05-21 05:45发布

问题:

I have an android program with 3 tabs. When I click on the “Map Tab” I want it to display “Map is Selected” message as shown in the picture bellow. But if I click on “Name” tab and then come back to the “Map Tab” the second time, the “Map is Selected Again” message wouldn’t show up in the screen. How can I make the “Map” tab display “Map is Selected Again” message each time I choose that tab? Can you edit the following code and show with an example about how to use 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;
} }

Updated. but the following method doesn't output a message.

`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:

You should implement OnTabChangeListener to the TabActivity class rather than the contents of the Tab.

In your TabActivity implement OnTabChangeListener

then set the listener for the TabHost mTabHost.setOnTabChangedListener(this);

@Override
    public void onTabChanged(String tabId) {
        Log.i("selected tab ", tabId);

    }

UPDATE

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());      
    }}