我有两个活动,一个是Mainactivity,另一个是Secondactivity。 Secondactivity包含从资源文件夹加载本地HTML页面的Webview。
Mainactivity包含标记为按钮A和按钮B的按钮,当按下它们将启动Secondactivity。我想将字符串作为URL从Mainactivity传递给Secondactivity,当按下按钮A和按钮B时,它会加载A.html和B.html。
目前,我在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 Class上,我有以下代码
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;
}
Fragment能够完美地完成哪些工作,但是如何让它适用于Activity to activity?
答案 0 :(得分:1)
在第一个活动中,你应该为这样的意图添加额外的参数:
// 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);
答案 1 :(得分:0)
如果您正在使用活动,请将该网址作为字符串传递并在下一个活动中获取。如下代码所示
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("url","somepage.com");
startActivity(intent);
在webView中,像这样
String url = getIntent().getStringExtras("url");
webView.load("url");
答案 2 :(得分:0)
制作一个加载网址的常见活动
例如:
public class WebviewActivity extends AppCompatActivity {
Activity mActivity;
WebView webview_;
String title ="";
String url ="";
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_webview_activity);
mActivity = this;
findviews();
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
title = bundle.getString("title");
url = bundle.getString("Url");
setAction();
}
else {
webview_.setVisibility(View.GONE);
}
}
private void setAction() {
try {
//check internet connection first
if (CommonUtils.isConnectingToInternet(WebviewActivity.this)) {
webview_.setVisibility(View.VISIBLE);
WebSettings webSettings = webview_.getSettings();
webview_.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
webview_.getSettings().setSupportZoom(true);
webview_.getSettings().setBuiltInZoomControls(true);
webview_.getSettings().setDisplayZoomControls(true);
webview_.loadUrl(url);
}
else {
//alert - no internet connection
}
} catch (Exception e) {
//print exp
e.printStackTrace();
}
}
private void findviews() {
webview_ = (WebView) findViewById(R.id.wbview);
}
public class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(dialog == null){
dialog = ProgressDialog.show(WebviewActivity.this, null, "Loading...");
dialog.setCancelable(true);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(dialog.isShowing())
dialog.dismiss();
}
}
}
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
tools:context=".activity.WebviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:supportsRtl="false"
app:popupTheme="@style/AppTheme.PopupOverlay">
<include layout="@layout/header_settinglayout" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wbview"
>
</WebView>
</RelativeLayout>
现在您可以从整个应用
加载任何网址或HTML来自活动:
if (CommonUtils.isConnectingToInternet(HelpActivity.this)) {
Intent i = new Intent(HelpActivity.this,WebviewActivity.class);
i.putExtra("title",getResources().getString(R.string.faqs));
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
startActivity(i);
}
else {
//alert - no internet
}
来自片段:
if (CommonUtils.isConnectingToInternet(getActivity())) {
Intent i = new Intent(getActivity(),WebviewActivity.class);
i.putExtra("title",getResources().getString(R.string.faqs));
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
getActivity().startActivity(i);
}
else {
//alert - no internet
}
Webview加载网址
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
Webview从资源目录加载html
i.putExtra("Url", "file:///android_asset/diploma.html");