本文介绍了如何使用OnTabChangeListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个标签的Andr​​oid程序。当我点击地图选项卡我希望它显示地图中选择了的消息如图片所示波纹管。但如果我点击名选项卡,然后再回来了地图选项卡的第二次,地图再次选择的消息不会在屏幕上显示出来。我怎样才能使地图选项卡显示每次地图又被选择的消息,我选择该选项卡?您可以编辑以下code,并与有关如何使用OnTabChangeListener一个例子说明了什么?

 公共类MapsActivity扩展MapActivity实现OnTabChangeListener {@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main4);
    Toast.makeText(getApplicationContext(),地图中选择Toast.LENGTH_LONG).show();
}公共无效onTabChanged(字符串tabId){
    Toast.makeText(getApplicationContext(),地图再次入选Toast.LENGTH_LONG).show();
}@覆盖
保护布尔isRouteDisplayed(){
    返回false;
}}

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

 `公共类HelloTabWidget扩展TabActivity实现OnTabChangeListener {`    私人TabHost mTab​​Host;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        资源解析度= getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec规范;
        意图意图;        意图=新意图()setClass(这一点,BarActivity.class)。
        规格= tabHost.newTabSpec(\"Name\").setIndicator(\"Name\",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
        tabHost.addTab(规范);        意图=新意图()setClass(这一点,CityActivity.class)。
        规格= tabHost.newTabSpec(\"city\").setIndicator(\"City\",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent);
        tabHost.addTab(规范);        意图=新意图()setClass(这一点,MapsActivity.class)。
        规格= tabHost.newTabSpec(\"Map\").setIndicator(\"Map\",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
        tabHost.addTab(规范);        tabHost.setCurrentTab(2);
    }    公共无效onTabChanged(字符串tabId){
        mTabHost = getTabHost();
        mTabHost.setOnTabChangedListener(本);
        Toast.makeText(getApplicationContext(),要检查显示,Toast.LENGTH_LONG).show();
        Log.i(选择选项卡索引,当前指数 - + mTab​​Host.getCurrentTab());
    }}


解决方案

您应该实施 OnTabChangeListener 来的TabActivity类,而不是选项卡的内容。

在您TabActivity实施OnTabChangeListener

然后设置监听器TabHost mTab​​Host.setOnTabChangedListener(本);

  @覆盖
    公共无效onTabChanged(字符串tabId){
        Log.i(选定的选项卡,tabId);    }

更新

 公共类HelloTabWidget扩展TabActivity实现OnTabChangeListener {`    私人TabHost mTab​​Host;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        资源解析度= getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec规范;
        意图意图;
        mTabHost = getTabHost();
        意图=新意图()setClass(这一点,BarActivity.class)。
        规格= tabHost.newTabSpec(\"Name\").setIndicator(\"Name\",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
        tabHost.addTab(规范);        意图=新意图()setClass(这一点,CityActivity.class)。
        规格= tabHost.newTabSpec(\"city\").setIndicator(\"City\",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent);
        tabHost.addTab(规范);        意图=新意图()setClass(这一点,MapsActivity.class)。
        规格= tabHost.newTabSpec(\"Map\").setIndicator(\"Map\",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
        tabHost.addTab(规范);        tabHost.setCurrentTab(2);
        mTabHost.setOnTabChangedListener(本);
    }    公共无效onTabChanged(字符串tabId){
        Toast.makeText(getApplicationContext(),选定的选项卡+ tabId,Toast.LENGTH_LONG).show();
        Log.i(选择选项卡索引,当前指数 - + mTab​​Host.getCurrentTab());
    }}

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());      
    }} 
解决方案

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

这篇关于如何使用OnTabChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:20