2 zxd 72 zxd_72 于 2013.03.29 10:10 提问

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

2个回答

android_ls
android_ls   2013.03.29 12:20
已采纳
 TabHost tabHost = getTabHost(); 
 tabHost.setOnLongClickListener(new OnLongClickListener(){}

看上面这两行,说明你的长按事件监听器是给TabHost对象绑定的,你改为为每个 TabHost.TabSpec (单个选项卡)绑定setOnLongClickListener(),或者为代表单个选项卡的View绑定长按事件监听器,你试试看。

Baby_Bonnie
Baby_Bonnie   2013.04.03 14:47

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

tabHost.getTabWidget().getChildAt(0).setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View v) {
        Toast.makeText(getApplicationContext(), "long click", 1).show();
        return true;
    }
});
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!