2016-03-08 42 views
-1

當按鈕點擊鏈接時如何在android中隱藏網頁不可用頁面?請任何可以發送代碼。如何在android中點擊按鈕時隱藏web不可用頁面?

我的代碼

package com.example.mysd;  
import android.content.Intent;  
import android.view.View.OnClickListener;  
import android.widget.Button; 



public class mysd extends Activity { 

Button button1; 

@Override 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_mysd); 

    addListenerOnButton1(); 

} 


public void addListenerOnButton1() { 


    button1 = (Button) findViewById(R.id.button1); 


    button1.setOnClickListener(new OnClickListener() { 

     @Override 

     public void onClick(View arg0) { 

      Intent browserIntent = 

         new Intent(Intent.ACTION_VIEW, 
Uri.parse("http://www.google.com/")); 

      startActivity(browserIntent); 

     } 

    }); 

} 
}         

回答

1

嘗試下面的代碼,

WebView web = (WebView) findViewById(R.id.web); 
progressBar = new ProgressDialog(Activity.this, "Loading..."); 

web.setWebViewClient(new WebViewClient() { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     progressBar.show(); 
    } 

    public void onPageFinished(WebView view, String url) { 
     if (progressBar.isShowing()) { 
      progressBar.dismiss(); 
     } 
    } 

    public void onReceivedError(WebView webView, int errorCode, 
           String description, String failingUrl) { 

     super.onReceivedError(webView, errorCode, description, failingUrl); 
     try { 
      webView.stopLoading(); 
     } catch (Exception e) {// 
     } 
     if (webView.canGoBack()) { 
      webView.goBack(); 
     } 
     AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create(); 
     alertDialog.setTitle("Error"); 
     alertDialog.setMessage("Error Message"); 
     alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, 
       "Try Again", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
         startActivity(getIntent()); 
        } 
       }); 
     alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
        } 
       }); 
     if (progressBar.isShowing()) { 
      progressBar.dismiss(); 
     } 
     alertDialog.show(); 
    } 
}); 
web.loadUrl("http://your_url"); 

請注意,在上面的代碼中一些功能已被棄用。而不是意圖在你的應用程序中使用webview。 (在活動的佈局文件中添加webview)

相關問題