用什麼方法可以在 tabs 上添加 setOnLongClickListener?或者點擊一個 tab 時,可以調用一個 activity。長時間點擊相同的 tab 時,調用一個不同的 activity?
public class HelloTabWidget 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();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
//// Intent i=new Intent(getApplicationContext(),LongClickStuff.class);
// startActivity(i);
// return true;
Toast.makeText(getApplicationContext(), "into long click", Toast.LENGTH_LONG).show();
return false;
}
});
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
TabHost tabHost = getTabHost();
tabHost.setOnLongClickListener(new OnLongClickListener(){}
看上面這兩行,說明你的長按事件監聽器是給TabHost對象綁定的,你改為為每個 TabHost.TabSpec (單個選項卡)綁定setOnLongClickListener(),或者為代表單個選項卡的View綁定長按事件監聽器,你試試看。