Android TabHost会打开多个标签

时间:2013-12-21 18:50:08

标签: android webview android-tabhost

我已经实现了一个tabHost,它托管了一些动态创建的webview。我想要的行为如下:

  • 当应用程序启动时,我只想显示一个选项卡 在里面我希望myUrl被加载。
  • 当我点击网页浏览中的链接时,我想要另一个标签 打开,我希望此选项卡显示新页面。

因此,我想要的行为实际上就像一个支持多个标签的简单浏览器。

我的代码是:

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity  {

    public TabActivity act=this;
    private String startUrl="myUrl";
    public ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
    public int counter=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        createTab(startUrl);

    }

    private void createTab(String url) {

        TabHost tabs = getTabHost();
        TabSpec spec=tabs.newTabSpec(Integer.toString(counter));
        spec.setIndicator(Integer.toString(counter));
        counter++;
        spec.setContent(new MyTabContentFactory(url)); 
        list.add(spec);
        tabs.addTab(spec);
        tabs.setCurrentTabByTag(spec.getTag());

    }

    public class MyTabContentFactory implements TabContentFactory{

        private String url;

        public MyTabContentFactory(String url) {
            this.url=url;
        }

        @Override
        public View createTabContent(String tag) {
                WebView browser=new WebView(act);
                browser.setWebViewClient(new MyWebViewClient());
                browser.getSettings().setJavaScriptEnabled(true);
                browser.getSettings().setBuiltInZoomControls(true);
                browser.loadUrl(url);
                return (browser);
        }
    }

    public class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            createTab(url);
            return false;

        }
    }

    @Override
    public void onBackPressed() {

        TabHost tabs = getTabHost();
        int id=tabs.getCurrentTab();
        list.remove(id); // remove it from memory  
        tabs.clearAllTabs();  // clear all tabs from the tabhost  
        if(list.size()>0)
            for(TabHost.TabSpec spec : list) // add all that you remember back  
                tabs.addTab(spec);
        else
            act.finish();

    }
}

此代码的行为是:

  • 打开后,我的网址已正确加载
  • 当我点击链接时,会创建新标签并加载页面,但第一个标签现在也会加载新页面。
  • 第二个问题是另一个标签会自动打开!

我整天都在努力解决这个问题,我无法弄清楚我的错误在哪里......有人能帮助我吗? 也建议一个很好的方法来实现这一点,将不胜感激!

谢谢!

编辑: 现在我的代码

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity  {

    private final static String TAG="MainActivity";
    public TabActivity act=this;
    private final static String startUrl="http://www.myurl.com";
    private ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
    private int counter=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.i(TAG,"onCreate");

        if(list.size()==0)
            createTab(startUrl);
        else{
            TabHost tabs = getTabHost();
            tabs.clearAllTabs();  // clear all tabs from the tabhost  
            for(TabHost.TabSpec spec : list) // add all that you remember back  
                tabs.addTab(spec);  
        }       
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.i(TAG,"onStart");
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Log.i(TAG,"onRestart");
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.i(TAG,"onResume");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.i(TAG,"onPause");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.i(TAG,"onStop");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)  {
        super.onSaveInstanceState(outState);
        Log.i(TAG,"onSaveInstanceState");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG,"onRestoreInstanceState");

    }


    private void createTab(String url) {
        Log.i(TAG,"createTab");
        TabHost tabs = getTabHost();
        TabSpec spec = tabs.newTabSpec(Integer.toString(counter));
        spec.setIndicator(Integer.toString(counter));
        spec.setContent(new MyTabContentFactory(url)); 
        list.add(spec);
        tabs.addTab(spec);
        tabs.setCurrentTab(counter);
        counter++;
    }

    @Override
    public void onBackPressed() {
        Log.i(TAG,"onBackPressed");
        TabHost tabs = getTabHost();
        int id=tabs.getCurrentTab();
        list.remove(id); // remove it from memory  
        counter--;
        tabs.clearAllTabs();  // clear all tabs from the tabhost 
        int indicator=0;
        if(list.size()>0)
            for(TabHost.TabSpec spec : list){ // add all that you remember back  
                spec.setIndicator(Integer.toString(indicator));
                indicator++;
                tabs.addTab(spec);
            }
        else
            act.finish();
    }

public class MyTabContentFactory implements TabContentFactory{

        private String url;

        public MyTabContentFactory(String url) {
            this.url=url;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public View createTabContent(String tag) {
                WebView browser=new WebView(act);
                browser.setWebViewClient(new MyWebViewClient());
                browser.getSettings().setJavaScriptEnabled(true);
                browser.getSettings().setBuiltInZoomControls(true);
                if(android.os.Build.VERSION.SDK_INT>10)
                    browser.getSettings().setDisplayZoomControls(false);
                browser.loadUrl(url);
                return (browser);
        }
    }

    public class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            Log.i(TAG,"shouldOverrideUrlLoading");
            Log.i(TAG,url);
            createTab(url);
            return true;
        }
    }
}

一切正常,但我不知道如何在屏幕方向改变时存储webview状态......任何提示?

0 个答案:

没有答案