我为我的企业开发了一个网站,并且几乎全部使用PHP进行工作。
因此,对于我来说,Java语言(和Android Studio)是一个非常新的东西。
尽管如此,我必须创建一个APK以使用该网站(为了在该网站上阻止Android Home)。
目前,我已经成功地设置了刷新页面,现在我正在尝试为使用WebView的应用程序添加进度/加载条,但它并不起作用……
MainActivity.java
因此,对于我来说,Java语言(和Android Studio)是一个非常新的东西。
尽管如此,我必须创建一个APK以使用该网站(为了在该网站上阻止Android Home)。
目前,我已经成功地设置了刷新页面,现在我正在尝试为使用WebView的应用程序添加进度/加载条,但它并不起作用……
MainActivity.java
import android.support.v4.widget.SwipeRefreshLayout;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView webView;
SwipeRefreshLayout swipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
LoadWeb();
}
public void LoadWeb(){
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("https://www.google.com/");
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onLoadResource(WebView view, String url) { //Doesn't work
swipe.setRefreshing(true);
}
public void onPageFinished(WebView view, String url){
//Hide the SwipeReefreshLayout
swipe.setRefreshing(false);
}
});
}
@Override
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}else {
finish();
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eu.test.testappli.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
Java对我来说可能看起来很简单,但仍然很晦涩,所以我无法实现在网上找到的代码片段。