2011-05-18 37 views
1

有沒有一種方法,我們可以在Tabs中添加setOnLongClickListener?或者有什麼其他方式可以在單擊標籤時調用一項活動,以及在同一標籤上長時間點擊時可以調用另一項活動?LongClick在標籤

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

回答

2

測試和認證,其中「0」是要長時間點擊選項卡的索引:我想這

tabHost.getTabWidget().getChildAt(0).setOnLongClickListener(new OnLongClickListener() { 
    public boolean onLongClick(View v) { 
     Toast.makeText(getApplicationContext(), "long click", 1).show(); 
     return true; 
    } 
}); 
+0

但它不工作。我會發布我的代碼。 – digvijay 2011-05-19 13:23:20

+0

地獄鐘聲。我剛剛運行了代碼。它的工作原理,但長期點擊標籤視圖,而不是標籤本身。有趣... – 2011-05-19 15:24:21

+0

更正以上的代碼。 – 2011-05-19 16:08:39