zxd_72 2013-03-29 02:10 采纳率: 0%
浏览 3029
已采纳

点击 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 2013-03-29 04:20
    关注
     TabHost tabHost = getTabHost(); 
     tabHost.setOnLongClickListener(new OnLongClickListener(){}
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器