웹view 진행률 표시줄 불러오기

4570 단어
많은 사람들이uc의 맨 윗부분의 진도표를 어떻게 실현하느냐고 물었기 때문에 나는 연구를 가서 쓸데없는 말은 하지 않고 바로 코드에 올렸다
//      
  
import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.Menu; 
import android.view.View; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.LinearLayout; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
  
public class MainActivity extends Activity { 
        Handler handler; 
        WebView wv; 
        TextView textView; 
        ProgressBar progressBar; 
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
//        requestWindowFeature(Window.FEATURE_NO_TITLE); 
//                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
//                                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
                LinearLayout rootViewLayout = new LinearLayout(this); 
                rootViewLayout.setOrientation(LinearLayout.VERTICAL); 
                textView = new TextView(this); 
                textView.setTextColor(Color.RED); 
                //        
                progressBar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal); 
                wv = new WebView(this); 
                rootViewLayout.addView(textView); 
                rootViewLayout.addView(progressBar); 
                rootViewLayout.addView(wv); 
        setContentView(rootViewLayout); 
         wv.getSettings().setAllowFileAccess(true);  
         wv.getSettings().setJavaScriptEnabled(true); 
         //       J8         ,     
        wv.loadUrl("http://www.12306.cn"); 
        wv.setWebViewClient(new WebViewClient(){ 
                        @Override
                        public void onPageFinished(WebView view, String url) { 
                                // TODO Auto-generated method stub 
                                super.onPageFinished(view, url); 
                                //      ,               
                                //WebChromeClient progress==100      
                                if (wv.getContentHeight() != 0) { 
                                        //          
                                } 
                        } 
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
                                // TODO Auto-generated method stub 
                                //       ,       
                                view.loadUrl(url); 
                                return true; 
                        } 
                          
        }); 
          
        wv.setWebChromeClient(new WebChromeClient(){ 
                @Override
                public void onProgressChanged(WebView view, int newProgress) { 
                        // TODO Auto-generated method stub 
                        super.onProgressChanged(view, newProgress); 
                        //   textView    progress      
//                        if (newProgress == 0) { 
//                                        textView.setVisibility(View.VISIBLE); 
//                                        progressBar.setVisibility(View.VISIBLE); 
//                                } 
                        textView.setText(newProgress+""); 
                        textView.postInvalidate(); 
                        progressBar.setProgress(newProgress); 
                        progressBar.postInvalidate(); 
//                        if (newProgress == 100) { 
//                                        textView.setVisibility(View.GONE); 
//                                        progressBar.setVisibility(View.GONE); 
//                                } 
                } 
        }); 
    } 
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 
}

좋은 웹페이지 즐겨찾기