是的,我讀了幾乎所有關於webview進度條的主題,嘗試幾乎所有東西。但他們不適合我,我的應用程序崩潰時,我使用的例子。Android WebView Onclick ProgressBar
我在我的webview應用程序中使用本地(ondevice)html,我想在鏈接點擊該html後顯示進度條(通知)。這是我的代碼。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dijitalyayincim.cekmekoy.cekmekoy_web.MainActivity">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
</RelativeLayout>
Main_Activity.java
package com.dijitalyayincim.cekmekoy.cekmekoy_web;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab =getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.mipmap.ic_launcher);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/www/index.html");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyAppWebViewClient.java
package com.dijitalyayincim.cekmekoy.cekmekoy_web;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by ahmet.sevinc on 18.11.2016.
*/
public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("cekmekoy.info")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
謝謝,我已經改變了About_Activity MainActivity,你的網址爲「file:///android_asset/www/index.html」,但現在我得到這個錯誤webView =(WebView)findViewById(R.id.webView);錯誤:(38,46)錯誤:無法找到符號變量webView – bahmet
您需要有一個具有webview元素的佈局文件 – vishnumm93
YESS當我將android:id =「@ + id/activity_main_webview」更改爲android:id =「@ + id/webview「在activity_main.xml中一切正常... – bahmet