以下に示す簡単なデモの例は、要件に応じていくつかの変更を加えることを確認してください
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class Tab_exActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
// this.myhost = (TabHost)this.findViewById(R.);
// this.myhost.setup();
final TabHost tabHost=getTabHost();
// The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, tabactivity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Home",
res.getDrawable(R.drawable.act1))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, tabactivity2.class);
spec = tabHost.newTabSpec("albums").setIndicator("Refresh",
res.getDrawable(R.drawable.act2))
.setContent(intent);
tabHost.addTab(spec);
// intent = new Intent().setClass(this, tabactivity3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Search",
res.getDrawable(R.drawable.act3))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
if (tabId.equals("songs")){
System.out.println("44444");
View v = tabHost.getCurrentView();
registerForContextMenu(v);
v.showContextMenu();
}
}});
// openContextMenu(spec);
// tabHost.setCurrentTab(2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
}