android 輸入html轉出網頁

目的:
可供使用者輸入html語法轉出成網頁


說明:
程式很簡單,直接貼上程式碼





注意事項:
編碼方式要注意

程式碼:

public static class PlaceholderFragment extends Fragment {
 Button go;
 EditText htmlin;
 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);
  htmlin = (EditText) rootView.findViewById(R.id.htmlin);
  wv = (WebView) rootView.findViewById(R.id.webView1);
  
  wv.getSettings().setJavaScriptEnabled(true);
  go.setOnClickListener(new OnClickListener() {   
   @Override
   public void onClick(View v) {
    String code = htmlin.getText().toString().trim();
    //將輸入的文字依UTF_8的編碼,及text/htm的格式轉成web格式‧
    try {
     wv.loadData(code, "text/html", HTTP.UTF_8);
    } catch (Exception e) {
     Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_SHORT).show();
    } 
   }
  });
  
  return rootView;
 }
}

留言

熱門文章