第二次单击选项卡时不调用活动

时间:2012-05-07 06:30:06

标签: android android-tabhost

我正在开发一个应用程序,我必须在其中显示五个选项卡,在每个选项卡上我调用另一个活动。在我的上一个选项卡中,我显示菜单。

主要问题是我的活动仅在标签更改事件上被调用。当我第一次点击最后一个标签时它会显示一个菜单,如果我再次单击该标签,则在选择任何菜单后没有任何事情发生。

那么如何解决这类问题呢?还有其他方法可以完成这项任务吗?任何帮助都会受到赞赏。谢谢

这是我的代码:

public class More extends ActivityGroup{

     Button btn1;
     ExpandableListView elw;
     Context ctx;
     public ListView modeList;
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ctx=this;

           Toast.makeText(More.this,"in more",200).show();
           setContentView(R.layout.more);

           AlertDialog.Builder builder = new AlertDialog.Builder(this);
           builder.setTitle("More Option");

            modeList = new ListView(this);
           String[] stringArray = new String[] { "About", "Settings","Invite By Message","Privacy Policy","Deactivate Account","Exit" };
           ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
           modeList.setAdapter(modeAdapter);


           builder.setView(modeList);
           final Dialog dialog = builder.create();

           dialog.show();
modeList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        String item=(String) modeList.getItemAtPosition(position);
        Toast.makeText(More.this,item,200).show();
        switch(position) {
        case 0:
            setContentView(R.layout.about);
            dialog.dismiss();
          TabHost tabHost = welcome.self.getTabHost();
          tabHost.setCurrentTab(4);

              // tabHost.setCurrentTab(4);
            break;
        case 1:
            setContentView(R.layout.settings);
            dialog.dismiss();
            break;
        case 2:
            setContentView(R.layout.messages);
            dialog.dismiss();
            break;
        case 3:
            setContentView(R.layout.policy);
            dialog.dismiss();
            break;
        case 4:
            Toast.makeText(More.this,"Deactivate the Account",200).show();
            dialog.dismiss();
            break;
        default:
             Toast.makeText(More.this,"Exit the application",200).show();
             dialog.dismiss();
             welcome.self.finish();
            }               
    }
});

2 个答案:

答案 0 :(得分:2)

TabHost tabs = (TabHost)findViewById(R.id.TabHost01);

        tabs.setup();

        TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");

        spec1.setContent(R.id.tab1);
        spec1.setIndicator("TAB1");

        tabs.addTab(spec1);

        TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("TAB2");

        tabs.addTab(spec2);

在您的方法中添加此内容,有关详细信息,请查看http://www.mkyong.com/android/android-tablayout-example/

如果您遇到问题,请告诉我,如果已解决,请点击右键。您的活动必须包含布局。

答案 1 :(得分:1)

modeList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        String item=(String) modeList.getItemAtPosition(position);
        Toast.makeText(More.this,item,200).show();
        switch(position) {
        case 0:
            setContentView(R.layout.about);
            dialog.dismiss();
         // TabHost tabHost = welcome.self.getTabHost();-> Intent abc;

         // tabHost.setCurrentTab(4);---->abc.getClass(More.this , NewClass.class);

              // tabHost.setCurrentTab(4);
            break;
        case 1:
            setContentView(R.layout.settings);
            dialog.dismiss();
            break;
        case 2:
            setContentView(R.layout.messages);
            dialog.dismiss();
            break;
        case 3:
            setContentView(R.layout.policy);
            dialog.dismiss();
            break;
        case 4:
            Toast.makeText(More.this,"Deactivate the Account",200).show();
            dialog.dismiss();
            break;
        default:
             Toast.makeText(More.this,"Exit the application",200).show();
             dialog.dismiss();
             welcome.self.finish();
            }               
    }
});

现在你的NewClass.java

public class NewClass extends TabActivit{


     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

           setContentView(R.layout.newclass);

Resources ressources = getResources(); 
        TabHost tabHost = getTabHost(); 

        // Android tab
        Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
        TabSpec tabSpecAndroid = tabHost
          .newTabSpec("Android")
          .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
          .setContent(intentAndroid);

        // Apple tab
        Intent intentApple = new Intent().setClass(this, AppleActivity.class);
        TabSpec tabSpecApple = tabHost
          .newTabSpec("Apple")
          .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
          .setContent(intentApple);

        // Windows tab
        Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
        TabSpec tabSpecWindows = tabHost
          .newTabSpec("Windows")
          .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
          .setContent(intentWindows);

        // Blackberry tab
        Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
        TabSpec tabSpecBerry = tabHost
          .newTabSpec("Berry")
          .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
          .setContent(intentBerry);

        // add all tabs 
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecApple);
        tabHost.addTab(tabSpecWindows);
        tabHost.addTab(tabSpecBerry);

        //set Windows tab as default (zero based)
        tabHost.setCurrentTab(2);
    }

}


}

这里有4个标签,您可以根据需要使用多个标签。 在android清单中注册。 确保布局文件中的tabhost具有与代码中相同的ID。

如果您接受我的回答,请单击右键。