android 基礎瀏覽器製作

目的:
利用WebView元件撰寫基礎的瀏覽器
可前進、後退、提示錯誤訊息

說明:
在Android不會特別說明元件屬性或UI編排方式
除非有特別的情況。


注意事項:
1.記得開啟Internet權限
2.進度條要在MainActivity的OnCreate設定
3.像youtue需要javaScript,記得設定開啟才能正常顯示

程式碼:
public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getWindow().requestFeature(Window.FEATURE_PROGRESS);
  //設定Activity顯示進度列,務必放在setContentView前
  //如此,才會有網頁讀取的進度條
  
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getFragmentManager().beginTransaction()
     .add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 //主Fragment---------------------------------------------------------------------------------
 public static class PlaceholderFragment extends Fragment {
  Button go,forward,back;
  EditText webAddress;
  WebView wv;
  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container,
     false);

   
   go =(Button) rootView.findViewById(R.id.go);
   forward = (Button) rootView.findViewById(R.id.forward);
   back = (Button) rootView.findViewById(R.id.back);
   webAddress =(EditText) rootView.findViewById(R.id.webAddress);
   wv =(WebView) rootView.findViewById(R.id.webView1);
   wv.getSettings().setJavaScriptEnabled(true);
   //開啟javaScript,才可以開youtube等網站
   
   wv.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
     getActivity().setProgress(newProgress*100);
     //修改Activity中的進度列     
     super.onProgressChanged(view, newProgress);
    }
   });
   
   wv.setWebViewClient(new WebViewClient(){
    @Override
    public void onReceivedError(WebView view, int errorCode,
      String description, String failingUrl) {
     Toast.makeText(getActivity(), "Sorry!"+description, Toast.LENGTH_SHORT).show();
     //顯示錯誤訊息
     super.onReceivedError(view, errorCode, description, failingUrl);
    }
   });
   
   OnClickListener listener = new OnClickListener() {  
    @Override
    public void onClick(View v) {
     switch (v.getId()) {
     case R.id.go:
      String url ="http://" + webAddress.getText().toString().trim();
      //trim() 去掉頭尾空白
      if(URLUtil.isNetworkUrl(url))
       wv.loadUrl(url);
      else
       Toast.makeText(getActivity(), "Sorry!網址錯誤",Toast.LENGTH_SHORT).show(); 
      break;
     case R.id.forward:
      if(wv.canGoForward())
       wv.goForward();
      else 
       Toast.makeText(getActivity(), "不可前進",Toast.LENGTH_SHORT).show();
      break;
     case R.id.back:
      if(wv.canGoBack())
       wv.goBack();
      else 
       Toast.makeText(getActivity(), "不可退回",Toast.LENGTH_SHORT).show();
      break;
     }
    }
   };
   go.setOnClickListener(listener);
   back.setOnClickListener(listener);
   forward.setOnClickListener(listener);
   
   return rootView;
  }
 }

}

留言

熱門文章