1

我的应用程序是一个新闻应用程序。由 5 个或更多选项卡组成(这将是基于每个用户要求的设置)。

当应用程序启动时,我动态创建 5 个选项卡并创建一个 web 视图作为每个选项卡的意图,我只需将每个选项卡的 URL 传递给意图,这是我的代码。

这是主要活动

package news.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.app.TabActivity;
import android.widget.TabWidget;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.content.Intent;

public class NewsMobile extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabHost = getTabHost();
        // Here I create the tabs dynamically.
        for(int i=0;i<5;i++){
        tabHost.addTab(
                tabHost.newTabSpec("tab"+i)
                .setIndicator("Politics")
                    // I need to pass an argument to the WebviewActivity to open a
                    // specific URL assume it is "http://mysite.com/?category="+i
                .setContent( new Intent(this, WebviewActivity.class)));
        }
        tabHost.setCurrentTab(0);
    }
}

这是我的 Webview Creator 活动

package news.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebviewActivity extends Activity {
    WebView browse;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        browse=new WebView(this);
        setContentView(browse);
                // I need the following line to read an argument and add it to the url
        browse.loadUrl("http://mysite.com/?category=");
    }
} 
4

2 回答 2

1

如果它对任何人有帮助,这里是@Shehabix 要求的额外信息:

所以开始 webview 活动,如接受的答案所示

Bundle bundle = new Bundle();
String url = "http://www.google.com";
bundle.putString("urlString", url);
Intent intent = new Intent(ThisActivity.this, NewActivity.class);
intent.putExtras(bundle);
startActivity(intent);

以下是如何在 webview 活动中截取此信息

public class MyWebView extends Activity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.asset_web_view);
        //get url stored in intent
        String url = super.getIntent().getExtras().getString("urlString");
        loadUrlInWebView(url);
    }

    private void loadUrlInWebView(String url){
        webView = (WebView) findViewById(R.id.mywebview);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
    }
}
于 2014-07-26T20:59:27.143 回答
1

你可以像这样使用Bundle

Bundle bundle = new Bundle();
String url = "http://www.google.com";
bundle.putString("urlString", url);
Intent intent = new Intent(ThisActivity.this, NewActivity.class);
intent.putExtras(bundle);
startActivity(intent);
于 2012-04-18T23:41:38.410 回答