gpt4 book ai didi

java - 如何在 android webview 加载的网站中添加链接或按钮单击加载器

转载 作者:行者123 更新时间:2023-12-01 09:46:53 26 4
gpt4 key购买 nike

我正在使用加载程序构建一个网页 View Android 应用程序,但我希望当用户单击网页 View 网站上的任何链接或按钮时显示加载程序直到响应出现,那么如何才能做到这一点。

public class MainActivity extends Activity {
/** Called when the activity is first created. */

WebView web;
ProgressBar progressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);

web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.example.com");
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);

progressBar.setVisibility(View.GONE);
}
}

// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

最佳答案

这里是如何解决您的问题的示例。 WebChromeClient提供加载监听器。更多信息here

webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100)
progressBar.setVisibility(View.GONE);
else {
progressBar.setProgress(newProgress);
progressBar.setVisibility(View.VISIBLE);
}
}
});

关于java - 如何在 android webview 加载的网站中添加链接或按钮单击加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37928551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com