我正在努力实现这个目标:MKyong - WebView.要获得确切的提示,请在"下载源代码"之前查看最后一张图片.
我的应用程序代码是:
bookingView = (WebView) findViewById(R.id.fullscreen_content);
bookingView.getSettings().setJavaScriptEnabled(true);
bookingView.loadUrl("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
但是该代码所做的是在android中的默认浏览器/内部浏览器中打开url(在本例中为Google)并且它并不意味着在我正在制作的android应用程序中.
有任何想法吗?
Pel*_*cho 11
在loadUrl()通话前添加此行
bookingView.setWebViewClient(new WebViewClient());
Run Code Online (Sandbox Code Playgroud)
这就是答案:
特征
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webView();
}
//Metodo llamar el webview
private void webView(){
//Habilitar JavaScript (Videos youtube)
webview.getSettings().setJavaScriptEnabled(true);
//Handling Page Navigation
webview.setWebViewClient(new MyWebViewClient());
//Load a URL on WebView
webview.loadUrl("http://stackoverflow.com/");
}
// Metodo Navigating web page history
@Override public void onBackPressed() {
if(webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
// Subclase WebViewClient() para Handling Page Navigation
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("stackoverflow.com")) { //Force to open the url in WEBVIEW
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main.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" >
<WebView android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在AndroidManifest.xml中包含它
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
结果
首页
WEBVIEW网站上的另一页
| 归档时间: |
|
| 查看次数: |
13156 次 |
| 最近记录: |