【问题标题】:How to add tabs dynamically that can be linked to a users choice of webpage如何动态添加可以链接到用户选择的网页的选项卡
【发布时间】:2010-12-01 00:58:24
【问题描述】:

我正在开发一个使用选项卡的应用程序,每个选项卡都链接到用户将能够使用 webview 看到并与之交互的网页。我遇到的问题是实现一个添加命令,用户可以使用该命令添加一个带有他们选择的 url 的选项卡,就像其他人一样工作

下面是我的代码

这是所有其他文件使用的主要 java 文件

public class UniversityofColorado extends TabActivity {


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

    TabHost host=getTabHost();

    host.addTab(host.newTabSpec("one")
            .setIndicator("Google")
            .setContent(new Intent(this, Hello.class)));

    host.addTab(host.newTabSpec("two")
                    .setIndicator("Colorado Main Site")
                    .setContent(new Intent(this, ColoradoMainSiteBrowser.class)));

    host.addTab(host.newTabSpec("three")
                    .setIndicator("CULearn")
                    .setContent(new Intent(this, CULearnBrowser.class)));

    host.addTab(host.newTabSpec("four")
            .setIndicator("CULink")
            .setContent(new Intent(this, CULinkBrowser.class)));

    host.addTab(host.newTabSpec("five")
            .setIndicator("MyCUInfo")
            .setContent(new Intent(this, MyCUInfoBrowser.class)));

    host.addTab(host.newTabSpec("six")
            .setIndicator("Campus Map")
            .setContent(new Intent(this, CampusBrowser.class)));

    host.addTab(host.newTabSpec("Seven")
            .setIndicator("Notes")
            .setContent(new Intent(this, Notepadv3.class)));
}   




    // Inflates menu when "menu Key" is pressed
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
}

然后我将每个网页定义在主文件调用的单独 java 文件中 以下是其中之一

public class ColoradoMainSiteBrowser extends Activity {

WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://colorado.edu/");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

我在主文件中定义了菜单,我只需要构造方法,以便按钮执行它们应该执行的操作。任何帮助都会很棒

【问题讨论】:

    标签: android webview dynamic add


    【解决方案1】:

    好的,我想我已经回答了你的问题here

    此外,您似乎喜欢复制类似的问题herehere

    就像我已经告诉过你的那样,你可以通过创建一个接受 url 作为额外意图的活动来完成此操作。

    以您的代码为基础:

    浏览器.java

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    
    public class Browser extends Activity {
    
        private WebView webview;
    
        private String URL;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.browser);
    
            Bundle extras = getIntent().getExtras();
    
            if (extras == null) {
    
                URL = "http://www.evonytools.org/";
    
            } else {
    
                this.URL = extras.getString("URL");
            }
    
            getWebView();
    
        }
    
        public void getWebView() {
    
            webview = (WebView) findViewById(R.id.webview);
            webview.setWebViewClient(new HelloWebViewClient());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl(this.URL);
        }
    
        private class HelloWebViewClient extends WebViewClient {
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
    
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
            }
        return super.onKeyDown(keyCode, event);
        }
    }
    

    布局/broswer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    >
        <WebView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/webview"
        />
    </LinearLayout>
    

    Main.java

    public class Main extends TabActivity{
    
            private TabHost tabHost;
    
            private EditText addressBar;
    
            private final static String DEFAULT_URL = "http://www.evonytools.org/";
    
            private int z = 0;
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.tabs_main);
    
                this.tabHost = getTabHost();  // The activity TabHost
                this.addressBar = (EditText) findViewById(R.id.address_bar);
                this.addressBar.setText(DEFAULT_URL);
    
    
                ImageButton addBtn = (ImageButton) findViewById(R.id.add_btn);
    
                addBtn.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                        addMethod();    
                    }
                });
    
                Intent openBrowser = new Intent(); 
                openBrowser.setClass(this, Browser.class);
                openBrowser.putExtra("URL", DEFAULT_URL);
                tabHost.addTab(tabHost.newTabSpec("Main").setIndicator(getHost(DEFAULT_URL)).setContent(openBrowser));
    
            }
    
    
    
    
            private void addMethod() {
    
                String webSiteURL = validateURL(addressBar.getText().toString().trim());
                String webSiteName = getHost(webSiteURL);
    
                Intent openBrowser = new Intent(); 
                openBrowser.setClass(this, Browser.class);
                openBrowser.putExtra("URL", webSiteURL);
    
                tabHost.addTab(tabHost.newTabSpec(webSiteName + Integer.toString(z)).setIndicator(webSiteName).setContent(openBrowser));
    
                ++z;
            }
    
            private void deleteMethod() {
    
                // Since we can't really delete a TAB
                // We hide it
    
                int position = tabHost.getCurrentTab();
    
                if (position != 0 ) {
    
                    tabHost.getCurrentTabView().setVisibility(8);
                    tabHost.setCurrentTab(0);
                }
            }
    
            // Inflates menu when "menu Key" is pressed
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.menu, menu);
                return true;
            }
    
            // This method is called once the menu is selected
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
    
    
                switch (item.getItemId()) {
    
                    case R.id.add:
    
                        addMethod();
    
                        break;
    
                    case R.id.delete:
    
                        deleteMethod();
    
                        break;
                }
                return true;
            }
    
            private String validateURL(String url) {
    
                StringBuffer urlB = new StringBuffer();
    
                // checks if addressBar has a valid URL
                // you can add stuff here in order to validate
                // this is just an example
                if (url.startsWith("http://")) {urlB.append(url);} else {urlB.append("http://");}
    
                try {
                    URL urlTry = new URL(urlB.toString());
    
                    return urlB.toString();
    
                } catch (MalformedURLException e) {
    
                    return "http://www.google.com/";
                }   
            }
    
            private String getHost(String url) {
    
                try {
    
                    URL urlTry = new URL(url);
    
                    return urlTry.getHost().replace("www.", "").replace(".com", "").replace(".org", "").replace(".net", "");
    
                } catch (MalformedURLException e) {
    
                    return "Browser";
                }
            }
    }
    

    tabs_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:tag="tabPane"
                 />
            <RelativeLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
                <EditText
                    android:id="@+id/address_bar"
                    android:layout_width="270px"
                    android:layout_height="50px"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                />
                <ImageButton
                    android:id="@+id/add_btn"
                    android:layout_width="50px"
                    android:layout_height="50px"
                    android:src="@android:drawable/ic_menu_add"
                    android:background="@android:color/transparent"
                    android:layout_toRightOf="@id/address_bar"
                />
    
            </RelativeLayout>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="2dp" />
        </LinearLayout>    
    

    希望我没有做你的功课。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2015-06-15
      • 2021-10-23
      • 2011-05-10
      • 2015-04-11
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多