本文介绍了如何将字符串url从Main Activity传递到下一个Activity并在webView中加载url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,一个是Mainactivity,另一个是Secondactivity. Secondactivity包含从资产文件夹加载本地HTML页面的Webview.

I have two Activity, One is Mainactivity and another is Secondactivity. Secondactivity contains Webview that loads local HTML pages from assets folder.

Mainactivity包含标记为Button A和Button B的按钮,当它们按下时将启动Secondactivity.我想将字符串作为URL从Mainactivity传递给Secondactivity,当按下Button A和Button B时,它将加载A.html和B.html.

Mainactivity contains buttons labeled as Button A and Button B when pressed would start Secondactivity. I would like to pass the string as URL from Mainactivity to Secondactivity which loads the A.html and B.html when Button A and Button B is pressed.

现在,我在Mainactivity类中有以下代码

For now, I have following code in Mainactivity Class

 Fragment firstFragment1 = new browser();
            Bundle args1 = new Bundle();
            args1.putString("url1", "file:///android_asset/diploma.html");
            firstFragment1.setArguments(args1);
            moveToFragment(firstFragment1);
            break;

在SecondActivity类上,我有以下代码

and on SecondActivity Class, I have following code

    String url1 = getArguments().getString("url1");


    myWebView=(WebView)rootView.findViewById(R.id.webview);
    myWebView.getSettings().setBuiltInZoomControls(true);

    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    myWebView.getSettings().setLoadsImagesAutomatically(true);
    myWebView.getSettings().setJavaScriptEnabled(true);

    myWebView.getSettings().setBuiltInZoomControls(true);

    myWebView.setInitialScale(1);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);

    WebSettings webSettings = myWebView.getSettings();


    myWebView.loadUrl(url1);


    return rootView;


}

哪些片段可以完美地工作,但是如何使它适用于Activity到Activity?

Which work for Fragment flawlessly, but how do I make it work for Activity to activity??

推荐答案

在第一个活动中,您应该为意图添加额外的参数,例如:

In first activity you should put extra argument to intent like this:

// I assume Web.class is your second activity
Intent intent = new Intent(this, Web.class); 
intent.putExtra("url", your_url); 
startActivity(intent);

然后在第二个活动中,您将检索如下参数:

String url = getIntent().getExtras().getString("url");
webView.load(url);

这篇关于如何将字符串url从Main Activity传递到下一个Activity并在webView中加载url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:41