调用 Visibility OnPageFinished - Android

Posted

技术标签:

【中文标题】调用 Visibility OnPageFinished - Android【英文标题】:call Visibility OnPageFinished - Android 【发布时间】:2014-05-15 16:24:02 【问题描述】:

页面停止加载时如何调用可见性? 我想让进度条不可见,但我使用的代码不允许我覆盖它:

public void onPageFinished(WebView view, String url) 
    findViewById(R.id.progressBar1).setVisibility(View.GONE);

活动:

public class Browser extends Activity 

    private WebView webView;
    private EditText urlEditText;
    private ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);

        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebChromeClient(new MyWebViewClient()); 

        CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
        webView.getSettings().setjavascriptEnabled(true);//Enable Java Script
        webView.setWebViewClient(new HelloWebViewClient());
        webView.loadUrl("https://www.facebook.com/"); //Set Home page
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
        webView.getSettings().setDefaultFontSize(12);//Set Font Size
        webView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
        webView.getSettings().setPluginState(PluginState.ON);//Enable Flash
        webView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
        // webView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 

        webView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
        webView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
        webView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
        webView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)

        progress = (ProgressBar) findViewById(R.id.progressBar1);
        progress.setMax(100);

        Button openUrl = (Button) findViewById(R.id.goButton);
        openUrl.setOnClickListener(new OnClickListener() 
            @Override
            public void onClick(View view) 
                String url = urlEditText.getText().toString();

                if (validateUrl(url)) 
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.loadUrl(url);
                    Browser.this.progress.setProgress(10);
                
            

            private boolean validateUrl(String url) 
                return true;
            
        );
    

    private class MyWebViewClient extends WebChromeClient 
        @Override
        public void onProgressChanged(WebView view, int newProgress)          
            Browser.this.setValue(newProgress);
            super.onProgressChanged(view, newProgress);
        
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    public void setValue(int progress) 
        this.progress.setProgress(progress);       
    

    private class HelloWebViewClient extends WebViewClient 
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url) 
            webview.loadUrl(url);
            return true;
        
    

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) 
            webView.goBack();
            return true;
        

        return super.onKeyDown(keyCode, event);
    


【问题讨论】:

【参考方案1】:

当 newProgress 等于 100 时,您可以在 progressChanged 方法上设置 Invisible。

【讨论】:

【参考方案2】:

你应该可以直接扔进去。

private class HelloWebViewClient extends WebViewClient 

    @Override
    public void onPageFinished(WebView view, final String url) 
        progress.setVisibility(View.GONE); // or View.INVISIBLE, whatever suits your needs
        // Or this for more clarity: Browser.this.progress.setVisibility(View.GONE);
    

    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) 

        webview.loadUrl(url);
        return true;
    

【讨论】:

以上是关于调用 Visibility OnPageFinished - Android的主要内容,如果未能解决你的问题,请参考以下文章

WPF 在 Window 关闭时,无法将 Visibility 设置为 Visible如何解决

transition与visibility与display

C#,WPF,Visibility类型为Visible后无法转为Collapsed,显示后隐藏

backface-visibility 属性

DIV中display和visibility属性差别

android:visibility