setCurrentTab of a tabHost

后端 未结 3 2021
别跟我提以往
别跟我提以往 2021-01-07 13:19

I\'m asking if we can call a setCurrentTab on a tabhost from another class than the class that contains the tabhost and tabspecs?

Can we put

tabHost.         


        
3条回答
  •  长情又很酷
    2021-01-07 14:07

    You sure can. For your situation, a singleton of the TabActivity may work best. For example:

    public class Main extends TabActivity{
        private staic Main theInstance;
    
        public static getInstance() {
            return Main.theInstance;
        }
    
        public Main() {
            Main.theInstance = this;
        }
    
        // The rest of your code here.
    }
    

    Then, from another class, you can just call:

    Main.getInstance().getTabHost().setCurrentTab(1);

    NOTE: What I have provided is not a complete singleton implementation, but it should suffice for what you're doing. Depending on the class you're setting the tab from, you may want to check that Main.getInstance() is not null before calling the setCurrentTab() method.

提交回复
热议问题