点击 tab 时调用 activity 的问题

用什么方法可以在 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绑定长按事件监听器,你试试看。

长时间点击Tab的时候 测试并验证哪个Tab的索引值是“0”

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