我试图在按钮点击时打开 WebView 。我想显示进度条并在WebView中设置默认视图。我应该做些什么改变? 此外,如果网页视图可以采取进度条的空间? 以下是我的代码。
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.dell.ca1.Birthdays">
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/background_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/background_dark"
android:orientation="horizontal">
<Button
android:id="@+id/archies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Archies >>"
android:textSize="10sp" />
<Button
android:id="@+id/fern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="FernsNPetals >>"
android:textSize="10sp" />
<Button
android:id="@+id/meeta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Gifts By Meeta >>"
android:textSize="10sp" />
</LinearLayout>
</HorizontalScrollView>
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@id/horizontalScrollView" />
<WebView
android:id="@+id/webview_bday"
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/horizontalScrollView" />
</LinearLayout>
此外,即使在按下后退后我仍然可以保留数据,并且只有当我点击&#34; archies&#34;按钮,查看产品,然后点击&#34; fernNpetals&#34;,然后点击&#34; archies&#34;按钮,然后我想查看产品页面而不是主页面。
java文件
package com.example.dell.ca1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
public class Birthdays extends AppCompatActivity {
Button btn_fnp,btn_archies,btn_meeta;
WebView webView;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthdays);
webView=(WebView)findViewById(R.id.webview_bday);
btn_archies=(Button)findViewById(R.id.archies);
btn_fnp=(Button)findViewById(R.id.fern);
// btn_igb=(Button)findViewById(R.id.igb);
btn_meeta=(Button)findViewById(R.id.meeta);
//progressBar=(ProgressBar)findViewById(R.id.pb);
OnClickButton();
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
}
private class MyBrowser extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
void OnClickButton()
{
btn_archies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl("https://www.archiesonline.com/");
}
});
btn_meeta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl("https://www.giftsbymeeta.com/");
}
});
btn_fnp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl("https://www.fnp.com/");
}
});
}
}
谢谢!
答案 0 :(得分:2)
您可以为webViewClient使用单独的类
例如,
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressLoading.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
progressLoading.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
progressLoading.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
progressLoading.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
}
然后调用webview,
myWebView.setWebViewClient(new MyWebViewClient());
请根据需要修改!!
快乐的java !!
答案 1 :(得分:0)
请尝试这样......
btn_archies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wv1.setWebViewClient(new MyBrowser());
wv1.loadUrl(url);
}
});
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog_webview = new ProgressDialog(Ppt_viewclass.this, R.style.MyTheme);
progressDialog_webview.setMessage("Please wait ...");
progressDialog_webview.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (progressDialog_webview != null) {
progressDialog_webview.dismiss();
}
}
}
我希望这能帮到你.....,一切顺利