[英]Webview link opens in external browser
我是制作android应用程序的初学者。 我遵循了本教程的有关如何进行网络视图的操作,并且在Galaxy Note 2上运行了该应用程序,没有出现任何错误,但是代码中编写的链接希望在打开应用程序时在外部浏览器中打开。 我希望页面在应用程序的Web视图内可见。 我该如何进行呢?
MainActivity.java
WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.wvwMain);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// load a webpage
browser.loadUrl("http://www.google.com/");
}
为了帮助您更好地理解,我在这里为您保留了简单的浏览器应用程序代码。
这里的SimpleBrowser是MainActivity。
public class SimpleBrowser extends Activity implements OnClickListener {
WebView ourBrowser;
EditText url;
Button go, go_Back, go_Forward, refresh, clr_History;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_webview);
ourBrowser = (WebView) findViewById(R.id.WVBrowser);
/* WebView Settings pretty important */
ourBrowser.getSettings().setJavaScriptEnabled(true);
ourBrowser.getSettings().setLoadWithOverviewMode(true);
ourBrowser.getSettings().setUseWideViewPort(true);
ourBrowser.setWebViewClient(new ourViewClient());
try {
ourBrowser.loadUrl("http://www.mybringback.com");
} catch (Exception e) {
e.printStackTrace();
}
go = (Button) findViewById(R.id.btn_Go);
go_Back = (Button) findViewById(R.id.btn_GoBack);
go_Forward = (Button) findViewById(R.id.btn_GoForward);
refresh = (Button) findViewById(R.id.btn_RefreshPage);
clr_History = (Button) findViewById(R.id.btn_ClrHistory);
url = (EditText) findViewById(R.id.eT_webbrowser);
go.setOnClickListener(this);
go_Back.setOnClickListener(this);
go_Forward.setOnClickListener(this);
refresh.setOnClickListener(this);
clr_History.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_Go:
String newWebaddress = url.getText().toString();
ourBrowser.loadUrl(newWebaddress);
/* Hiding the keyboard after the EditText data */
InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
break;
case R.id.btn_GoBack:
if (ourBrowser.canGoBack()) {
ourBrowser.goBack();
}
break;
case R.id.btn_GoForward:
if (ourBrowser.canGoForward()) {
ourBrowser.goForward();
}
break;
case R.id.btn_RefreshPage:
ourBrowser.reload();
break;
case R.id.btn_ClrHistory:
ourBrowser.clearHistory();
break;
}
}
}
然后再创建一个名称为ourViewClient.java的Java文件,该文件应包含以下代码:
public class ourViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true; // as mentioned in below notes, for your case., you do 'return false'
}
}
Android清单文件:确保声明了<uses-permission android:name="android.permission.INTERNET" />
。 browser_webview.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/eT_webbrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Go" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_GoBack"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Go back a Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_GoForward"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Go Forward"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_RefreshPage"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:text="Refresh Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_ClrHistory"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:text="Clear History"
android:textSize="@dimen/mediumsize" />
</LinearLayout>
<WebView
android:id="@+id/WVBrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
</LinearLayout>
您只需使用以下方法解决此问题:
myWebView.setWebViewClient(new HelloWebViewClient());
...
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("yoursite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
将此添加到您的oncreate()中:
browser.setWebViewClient(new WebViewClient()); //open urls inside browser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.