为什么这在更改 Web 视图的 URL 时不起作用(已解决但它现在正在打开一个新的浏览器 window 而不是将其保留在应用程序中???)
Why won't this work at changing the URL of a web View (resolved but it is now opening a new browser window instead of keeping it in the app???)
我正在制作作品集,我希望它更改为新的 URL 以在您单击“下一步”或 nextButton(mNextButton 变量)后加载。这是带有 webview 的 activity 的代码。
package com.dredaydesigns.dredaycreativeportfolio;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Button;
import android.R.id;
import static com.dredaydesigns.dredaycreativeportfolio.R.layout.activity_content_activity;
public class content_activity extends ActionBarActivity {
private WebView mWebView;
private Button mNextButton;
private String webURL = "http://extremesportsshows.com/"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_content_activity);
String backgroundColor = getIntent().getExtras().getString("webColor");
ImageView iv = (ImageView) findViewById(R.id.subBanner);
iv.setBackgroundColor(Color.parseColor(backgroundColor));
mWebView = (WebView) findViewById(R.id.webView);
mWebView.loadUrl("webURL");
mNextButton= (Button) findViewById(R.id.nextButton);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webURL = "http://www.betterup.co/";
}
});
}
这是 activity_content_activity
的 xml 代码
'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#0e6fa2">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pageBanner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/webactivityheader02"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/subBanner"
android:layout_below="@+id/pageBanner"
android:layout_alignLeft="@+id/pageBanner"
android:layout_alignStart="@+id/pageBanner"
android:adjustViewBounds="false"
android:padding="8dp"
android:background="#C41E72"/>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_below="@+id/subBanner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="87dp"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Website done for an agency that representse extreme sports athletes. Video overlay, HTML 5, JOOMLA"
android:id="@+id/contentTextView"
android:layout_marginBottom="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="@+id/nextButton"
android:layout_above="@+id/contentTextView"
android:layout_centerHorizontal="true"/>
</RelativeLayout>'
首先,您的 WebView 根本不会加载网页,因为以下行
mWebView.loadUrl("webURL");
错了。您应该将一个字符串对象传递给存储在 webURL
字符串中的 loadUrl() method, and that string should be the url that you want to load. In your case its http://extremesportsshows.com/。所以你的代码应该是这样的。
mWebView.loadUrl(webURL); // Without the quotes
点击下一步后,您将再次加载 WebView 中的 url,如下所示
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webURL = "http://www.betterup.co/";
mWebView.loadUrl(webURL);
}
});
我正在制作作品集,我希望它更改为新的 URL 以在您单击“下一步”或 nextButton(mNextButton 变量)后加载。这是带有 webview 的 activity 的代码。
package com.dredaydesigns.dredaycreativeportfolio;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Button;
import android.R.id;
import static com.dredaydesigns.dredaycreativeportfolio.R.layout.activity_content_activity;
public class content_activity extends ActionBarActivity {
private WebView mWebView;
private Button mNextButton;
private String webURL = "http://extremesportsshows.com/"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_content_activity);
String backgroundColor = getIntent().getExtras().getString("webColor");
ImageView iv = (ImageView) findViewById(R.id.subBanner);
iv.setBackgroundColor(Color.parseColor(backgroundColor));
mWebView = (WebView) findViewById(R.id.webView);
mWebView.loadUrl("webURL");
mNextButton= (Button) findViewById(R.id.nextButton);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webURL = "http://www.betterup.co/";
}
});
}
这是 activity_content_activity
的 xml 代码'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#0e6fa2">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pageBanner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/webactivityheader02"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/subBanner"
android:layout_below="@+id/pageBanner"
android:layout_alignLeft="@+id/pageBanner"
android:layout_alignStart="@+id/pageBanner"
android:adjustViewBounds="false"
android:padding="8dp"
android:background="#C41E72"/>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_below="@+id/subBanner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="87dp"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Website done for an agency that representse extreme sports athletes. Video overlay, HTML 5, JOOMLA"
android:id="@+id/contentTextView"
android:layout_marginBottom="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="@+id/nextButton"
android:layout_above="@+id/contentTextView"
android:layout_centerHorizontal="true"/>
</RelativeLayout>'
首先,您的 WebView 根本不会加载网页,因为以下行
mWebView.loadUrl("webURL");
错了。您应该将一个字符串对象传递给存储在 webURL
字符串中的 loadUrl() method, and that string should be the url that you want to load. In your case its http://extremesportsshows.com/。所以你的代码应该是这样的。
mWebView.loadUrl(webURL); // Without the quotes
点击下一步后,您将再次加载 WebView 中的 url,如下所示
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webURL = "http://www.betterup.co/";
mWebView.loadUrl(webURL);
}
});