我是制作Android应用程序的初学者。我按照THIS教程了解了如何制作webview,我在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/");
}
答案 0 :(得分:3)
为了帮助您更好地理解,我将为您留下简单的浏览器应用代码。
此处 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,它应该包含以下代码:
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>
答案 1 :(得分:2)
你只需解决这个问题:
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;
}
}
答案 2 :(得分:1)
在你的oncreate()中添加:
browser.setWebViewClient(new WebViewClient()); //open urls inside browser