android中的进度条问题

时间:2013-08-18 06:26:39

标签: android progress-bar webviewclient

我在代码中添加了一个进度条来显示webview的进度。但进度条没有显示出来。我哪里弄错了?请指教。我复制了下面的完整代码。此代码有4个按钮,当单击按钮时,webview将显示该特定站点。我试图在加载网站时添加进度条。

public class MainActivity extends Activity   {
      /** Called when the activity is first created. */
    ImageButton imageButton;
    ProgressBar progressBar;

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    atebutton();
    blogsbutton();
    searchbutton();
    minkbutton();
    techmbutton();


    WebView webView = (WebView) findViewById(R.id.MyWebView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    progressBar.setMax(100);
   // webView.setInitialScale(67);
    WebSettings webSettings = webView.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptEnabled(true);

    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);


    webView.setWebViewClient(new WebViewClient() {
        public void onProgressChanged(WebView view, int progress) 
        {
        if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
            progressBar.setVisibility(ProgressBar.VISIBLE);
        }
        progressBar.setProgress(progress);

        if(progress == 100) {
            progressBar.setVisibility(ProgressBar.GONE);
        }
     }
        @Override 
        public void onReceivedLoginRequest(WebView view, String realm, 
                        String account, String args) { 


                System.err.println(realm); 
                System.err.println(account); 
                System.out.println(args); }

        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
            Toast.makeText(view.getContext(), "Unknown Error", Toast.LENGTH_LONG).show();
            System.err.println(errorCode + " - " + description + "-" + failingUrl); 


        }
         @Override
            public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
              System.err.println("SSL ERROR");  

             handler.proceed();
            }
      @SuppressLint({ "InlinedApi", "NewApi" })
    @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          if( (url.contains(".pdf"  )) || (url.contains(".doc"  )) || 
                  (url.contains(".xls"  )) || (url.contains(".ppt"  ))|| (url.contains(".txt"  ))){
          Intent DownloadIntent = null;
            try {
                  DownloadIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                  DownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
              } catch (URISyntaxException e) {
                  Log.e("Link Tag", e.getMessage());
              }
              startActivity(DownloadIntent);


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

        progressBar.setVisibility(View.GONE);
    }


      @Override
      public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host,
          String realm) {
          System.err.println("HTTP auth request"); 
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);        


        if (handler.useHttpAuthUsernamePassword()) {
          if (preferences.contains("username")) {
            handler.proceed(preferences.getString("username", null), preferences.getString("password", null));
            return;
          }

        }

        new Dialog(MainActivity.this){
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTitle(host);
            setContentView(R.layout.credentials);


            final EditText userName = (EditText) findViewById(R.id.UserName);
            final EditText password = (EditText) findViewById(R.id.Password);
            userName.setText(preferences.getString("username", ""));
            password.setText(preferences.getString("password", ""));

            Button button = (Button) findViewById(R.id.GoButton);
            button.setOnClickListener(new Button.OnClickListener(){

                public void onClick(View v) {
                String userName2 = userName.getText().toString();
                String password2 = password.getText().toString();
                Editor edit = preferences.edit();
                edit.putString("username", userName2);
                edit.putString("password", password2);
                edit.commit();
                handler.proceed(userName2, password2);
                dismiss();
              }});
          }

        }.show();
      }
    });

    String url;
    if (getIntent().hasExtra("url")) {
      url = getIntent().getStringExtra("url");
    } else {


     url = "https://kee.mahindrasatyam.com/_layouts/mobile/mobilesearch.aspx";


    }
    webView.loadUrl(url);
    MainActivity.this.progressBar.setProgress(0);


  }



    private void techmbutton() {
        // TODO Auto-generated method stub

        imageButton = (ImageButton) findViewById(R.id.imageButton5);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WebView webView = (WebView) findViewById(R.id.MyWebView);
                progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                   progressBar.setMax(100);
                webView.loadUrl("http://techmahindra.com");
            }
        });}



    private void blogsbutton() {
        // TODO Auto-generated method stub

        imageButton = (ImageButton) findViewById(R.id.imageButton2);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WebView webView = (WebView) findViewById(R.id.MyWebView);
                progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                   progressBar.setMax(100);
                webView.loadUrl("https://blogs.mahindrasatyam.com/m");
            }
        });}
    private void searchbutton() {
        // TODO Auto-generated method stub

        imageButton = (ImageButton) findViewById(R.id.imageButton3);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WebView webView = (WebView) findViewById(R.id.MyWebView);
                progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                   progressBar.setMax(100);
                webView.loadUrl("https://kee.mahindrasatyam.com/_layouts/mobile/mobilesearch.aspx");
            }
        });}

    private void minkbutton() {
        // TODO Auto-generated method stub

        imageButton = (ImageButton) findViewById(R.id.imageButton4);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WebView webView = (WebView) findViewById(R.id.MyWebView);
                progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                   progressBar.setMax(100);
                webView.loadUrl("https://mink.mahindrasatyam.com/m");
            }
        });}
    private void atebutton() {
        // TODO Auto-generated method stub

        imageButton = (ImageButton) findViewById(R.id.imageButton1);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WebView webView = (WebView) findViewById(R.id.MyWebView);
                   progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                   progressBar.setMax(100);
                webView.loadUrl("https://ate.mahindrasatyam.com/m");
            }
        });}



@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
    return true;
  }

1 个答案:

答案 0 :(得分:0)

您的代码没有progressBar.show();,这就是为什么progressBar没有显示在屏幕上。

例如,如果您想显示在​​网站上搜索某个字词的进度,请致电progressBar.show(),一旦完成,您应该致电progressBar.dismiss()将其隐藏在屏幕上。