【发布时间】:2017-05-01 07:40:51
【问题描述】:
我有一个简单的应用程序,其中包含一个编辑文本、5 个按钮、一个进度条和一个 WebView。我有另一个类名ourClient,它扩展了WebViewClient,我使用这个类设置为我的webviewclient 并加载url。问题是我想使用progressBar 和几乎所有示例在stackoverflow 和互联网上使用WebChromeClient 的方法onProgressChanged 显示进度,但在我的情况下,我没有使用WebChromeClient 类,我使用的是WebViewClient 类。在这个类中,我有 onPageStarted 和 onPageFinished 方法,但我如何使用它们来显示进度。
记住进度条在我的 MainActivity 中,而 onPageStarted 和 onPageFinished 方法在另一个类中。
这里是xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="13"
tools:context="com.hbss.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/etUrl"
android:layout_weight="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"
android:id="@+id/btnGo"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back"
android:id="@+id/btnBack"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward"
android:id="@+id/btnForward"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear"
android:id="@+id/btnClear"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reload"
android:id="@+id/btnReload"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wb"
/>
</LinearLayout>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/pb"
android:max="100"
android:indeterminate="false"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>
这里是 Java 代码。
MainActivity.Java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etUrl;
Button btnGo, btnBack, btnForward, btnClear, btnReload;
WebView wb;
String url;
ProgressBar pb;
ourClient oc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.pb);
pb.setVisibility(View.INVISIBLE);
wb = (WebView) findViewById(R.id.wb);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.setWebViewClient(new ourClient());
etUrl = (EditText) findViewById(R.id.etUrl);
btnGo = (Button) findViewById(R.id.btnGo);
btnBack = (Button) findViewById(R.id.btnBack);
btnForward = (Button) findViewById(R.id.btnForward);
btnClear = (Button) findViewById(R.id.btnClear);
btnReload = (Button) findViewById(R.id.btnReload);
btnBack.setEnabled(false);
btnForward.setEnabled(false);
btnGo.setOnClickListener(this);
btnReload.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnForward.setOnClickListener(this);
btnClear.setOnClickListener(this);
etUrl.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction()==KeyEvent.ACTION_DOWN && i==KeyEvent.KEYCODE_ENTER) {
load();
return true;
}
return false;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnGo:
load();
break;
case R.id.btnBack:
if (wb.canGoBack())
wb.goBack();
break;
case R.id.btnForward:
if (wb.canGoForward())
wb.goForward();
break;
case R.id.btnReload:
wb.reload();
break;
case R.id.btnClear:
wb.clearHistory();
break;
}
}
public void load(){
url = etUrl.getText().toString();
if (!url.startsWith("http://")) {
url = "http://" + url;
}
try {
wb.loadUrl(url);
wb.requestFocus();
View view = this.getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
if (wb.canGoBack()){
btnBack.setEnabled(true);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), " " + e, Toast.LENGTH_LONG).show();
}
}
}
ourClient.java
public class ourClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
}
【问题讨论】:
-
你到底在问什么?如何将
ProgressBar传递给WebViewClient? -
想在加载页面时通过progressBar显示进度。
-
如果您的意思是要显示 determinate 进度,请使用
WebChromeClient。我不知道你为什么这么坚决反对使用一个。 -
这是否意味着我不需要 ourClient.java 类并且可以使用 Webview.setWebChromeClient(new WebChromeClient) 设置 webclient?
-
是的,当然。我的意思是,除非您也需要
WebViewClient来做其他事情。您可以同时使用两者。