gpt4 book ai didi

java - 如何使用 OnTabChangeListener?

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:05 24 4
gpt4 key购买 nike

我有一个带有 3 个选项卡的 android 程序。当我单击“ map 选项卡”时,我希望它显示“ map 已选中”消息,如下图所示。但是,如果我单击“名称”选项卡,然后第二次返回“ map 选项卡”,则屏幕上不会显示“再次选择 map ”消息。每次选择该选项卡时,如何使“ map ”选项卡显示“再次选择 map ”消息?您能否编辑以下代码并举例说明如何使用 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());
}}

最佳答案

您应该为 TabActivity 类而不是 Tab 的内容实现 OnTabChangeListener

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

关于java - 如何使用 OnTabChangeListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com