2011-05-18 48 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