1

我希望您能提供帮助,如果当前选择了一个选项卡然后用户再次单击它,我似乎无法重新加载当前选项卡,有人可以指出我在做什么,也许可以修改我的代码以向我展示我做错了什么,因为我在这里和通过谷歌查看了很多线程,但现在似乎知道答案,或者我只是愚蠢:D谢谢:)

public class HelloTabWidget extends TabActivity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

// 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);

// 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(0);

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

public void onTabChanged(String tabId) {
   // Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());

    switch (tabHost.getCurrentTab()) {
    case 0:
        //do what you want when tab 0 is selected
            test();
        break;


    case 1:
        //do what you want when tab 1 is selected
        break;
    default:

        break;
    }
}
});     
}

public void test (){

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
4

1 回答 1

0

首先在应用程序类中创建一个变量,如下所示:

public class GlobalClass extends Application {
    public int displayTab = 0;

    public int getDisplayTab() {
        return displayTab;
    }

    public void setDisplayTab(int displayTab) {
        this.displayTab = displayTab;
    }
}

并修改您的活动如下:

    public class HelloTabWidget extends TabActivity implements OnClickListener {


    GlobalClass globel;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    globel = (GlobalClass) getApplication();

    Resources res = getResources(); // Resource object to get Drawables
    final TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // 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);

    // 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(globel.getDisplayTab());


//Here Handling first tab click 
         getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                                test();
                            Log.d("Clickedddddddddddd", "1"
                                    + getTabHost().getCurrentTabTag());
                            HelloTabWidget.this.finish();
                            globel.setDisplayTab(0);
                            Intent i = new Intent(HelloTabWidget.this, HelloTabWidget.class);
                            startActivity(i);

                        }
                    });

    }
    public void test (){

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Warning");
    alert.setMessage("You are about to self-destruct!");
    alert.show();
    }

    }
于 2012-05-18T11:06:56.123 回答