Basic use of WebVeiw and WebVeiw
Today, let's take a look at WebView.
There are two possible scenarios: 1 in fragment, 2 in Activity
1. It is difficult to roll back the WebView Client in Fragment.
Solution Code:
The principle is simple: Listen to the current Fragment event, but when you press it, check whether it is a rollback key. Can WebView be rolled back?
If you can, roll back!
// Set the WebView internal rollback jump: webView. setOnKeyListener (new OnKeyListener () {@ Overridepublic boolean onKey (View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (event. getAction () = KeyEvent. ACTION_DOWN) {if (keyCode = KeyEvent. KEYCODE_BACK & webView. canGoBack () {webView. goBack (); return true ;}} return false ;}});
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Package com. cim. pd. ui. fragment; import android. app. activity; import android. OS. bundle; import android. support. annotation. nullable; import android. support. v4.app. fragment; import android. view. keyEvent; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. view. onKeyListener; import android. view. viewGroup; import android. webkit. webView; import Android. widget. imageView; import android. widget. progressBar; import com. cim. pd. r; import com. cim. pd. ui. mainActivity; import com. cim. pd. view. myWebViewClient;/*** menu bar: * purchase the ximeng smart device page ** @ author Hades * @ Time 2014-12-4 **/public class BuyDeviceFragment extends Fragment {public static final String ARG = "BuyDeviceFragment"; private MainActivity mainActivity; /*** WebView of the purchased device **/private WebView webView; /*** Progress bar **/private ProgressBar progressBar; @ Overridepublic void onAttach (Activity activity) {super. onAttach (activity); mainActivity = (MainActivity) activity;} @ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState);} @ Overridepublic View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedI NstanceState) {// TODO Auto-generated method stubView view = inflater. inflate (R. layout. fragment_buy_device, container, false); initTop (view); initWebView (view); return view ;} /*** initialize the information at the top ** @ author Hades * @ Time 2014-12-4 * @ param view */private void initTop (View view) {progressBar = (ProgressBar) view. findViewById (R. id. buy_progressBar); progressBar. setVisibility (View. VISIBLE); ImageView drawerView = (ImageView) view. findViewById (R. id. drawer_menu); // set the listener: drawerView. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubmainActivity. toggle () ;}});}/*** initialization page information: ** @ author Hades * @ Time 2014-12-4 * @ param view */private void initWebView (View view) {webView = (WebView) view. findViewById (R. id. buy_webview); webView. getSettings (). setJavaScrip TEnabled (true); String url = "http://m.cim120.com/"; // Step 1: load the web site webView directly. loadUrl (url); // Step 2: Set the page to jump to the current WebView! MyWebViewClient client = new MyWebViewClient (mainActivity, progressBar); webView. setWebViewClient (client); // sets the WebView internal rollback redirect: webView. setOnKeyListener (new OnKeyListener () {@ Overridepublic boolean onKey (View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (event. getAction () = KeyEvent. ACTION_DOWN) {if (keyCode = KeyEvent. KEYCODE_BACK & webView. canGoBack () {webView. goBack (); return true ;}}}) ;}@ Overridepublic void onDestroyView () {// TODO Auto-generated method stubsuper. onDestroyView ();}}
2 Activity
/*** Initialize the component ** @ Hades * @ Dec 3, 2014 * @ 11:35:35 PM */private void init () {webView = (WebView) this. findViewById (R. id. my_buy_webview); webView. getSettings (). setJavaScriptEnabled (true); String url = "http://m.cim120.com/"; // Method 1: Load webView directly. loadUrl (url); // type 2: Create your own browser! Souyou's data information is displayed in it! MyWebViewClient client = new MyWebViewClient (BuyDeviceActivity. this, progressBar); webView. setWebViewClient (client);}/*** sets the internal rollback of WebViewClient */@ Overridepublic void onBackPressed () {// TODO Auto-generated method stubif (webView. canGoBack () {webView. goBack () ;}else {finish ();}}
The Activity has a callback method that listens for the rollback key.
OnBAckPressed;
Else ---------------------------------------------------------------------------------------------------------------------------------
WebView. getSettings (). setJavaScriptEnabled (true );
This is the support for adding Js. Without this sentence, it will cause the webView to load the page,
JS special effects cannot be displayed!
Bytes -------------------------------------------------------------------------------------------------------------------------------
Today, some minor projects:
When you jump to BuyDeviceActivity
How can I save the data when I click it?
Error:
12-04 13:51:19. 778: E/CrashHandler (19281): Caused by: libcore. io. ErrnoException: write failed: ENOSPC (No space left on device)
The reason is: I'm too happy! No Activity is added to the Application!
Bytes ------------------------------------------------------------------------------------------------------------------------
WebViewClient is the data used to display the content of WebView;
If no such content exists, When you click the content in WebView, an Intent will be enabled by default to call the local
Browser App.
Therefore, you must customize WebViewClient and shouldOverrideUrlLoading must be overwritten.
Package com. example. webview; import android. app. activity; import android. content. context; import android. util. attributeSet; import android. view. view; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. progressBar; public class MyWebViewClient extends WebViewClient {private ProgressBar progressBar; public MyWebViewClient () {// TODO Auto-generated constructor stub} public MyWebViewClient (Activity context) {// TODO Auto-generated constructor stubsuper ();} public MyWebViewClient (Activity context, ProgressBar progressBar) {// TODO Auto-generated constructor stubsuper (); this. progressBar = progressBar;} @ Overridepublic boolean shouldOverrideUrlLoading (WebView view, String url) {// TODO Auto-generated method stubview. loadUrl (url); return true;}/*** WebView loaded data **/@ Overridepublic void onPageFinished (WebView view, String url) {// TODO Auto-generated method stub // super. onPageFinished (view, url); progressBar. setVisibility (View. GONE );}}
OnPageFinished () is called after the WebView is loaded successfully,
You can set the progress bar to gone!
Make the progress bar disappear.