Android recording 24-WebView realize daytime/night reading mode, android24-webview

Source: Internet
Author: User
Tags button type

Android recording 24-WebView realize daytime/night reading mode, android24-webview
Preface

This blog will share with you the use case of WebView to enable Android to call JavaScript code to control the daytime/nighttime mode. The official website provides a good description of how to use WebView. Building Web Apps in WebView is not described here.

Implementation

Unlike Native applications, we can easily call the system API, that is, set the topic by style. How can we achieve this if our content is in html? First, WebView is required to load html pages. You can use loadUrl to display html pages to webView. We know that Android can interact with JavaScript, that is to say, Android code can be called in JavaScript, or JavaScript code can be called in Android,Android calls the JavaScript code in the Html page to control the page background and font style to switch between day and night modes..

How to provide an html code for testing
<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd";> 
Load html pages
WebSettings settings = webView. getSettings (); // you can set settings. setJavaScriptEnabled (true) for javaScript. webView. loadUrl ("file: // android_asset/01.html ");
Android calls javascript code
@Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.btn_nightmode:            webView.loadUrl("javascript:load_night()");            break;        case R.id.btn_lightmode:            webView.loadUrl("javascript:load_day()");            break;        default:            break;        }    }
Final Effect

Complete code
Package com. infzm. webview; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. webkit. webSettings; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private WebView webView; private Button nightModeBtn; private Button lightModeBtn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); webView = (WebView) this. findViewById (R. id. webview); nightModeBtn = (Button) this. findViewById (R. id. btn_nightmode); lightModeBtn = (Button) this. findViewById (R. id. btn_lightmode); nightModeBtn. setOnClickListener (this); lightModeBtn. setOnClickListener (this); // webView. loadUrl ("http://www.baidu.com"); WebSettings settings = webView. getSettings (); // set settings available for javaScript. setJavaScriptEnabled (true); // bind the javaScript interface to call our Android code in javaScript // webView. addJavascriptInterface (new WebAppInterface (this), "Android"); // webView. setWebViewClient (new MyWebViewClient (); // load the html page webView under the assets Directory. loadUrl ("file: // android_asset/01.html ");} /*** used to control the page navigation * @ author wwj_748 **/private class MyWebViewClient extends WebViewClient {/*** when used to click the link, the system calls this method */@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {if (Uri. parse (url ). getHost (). equals ("www.baidu.com") {// This Is My webpage, so do not overwrite it. Let my WebView load the page return false;} // otherwise, this link is not my website page, so enable the browser to process urls Intent intent = new Intent (Intent. ACTION_VIEW, Uri. parse (url); startActivity (intent); return true ;}@override public boolean onKeyDown (int keyCode, KeyEvent event) {// check whether the event is returned, if there is a Web page history if (keyCode = KeyEvent. KEYCODE_BACK & webView. canGoBack () {webView. goBack (); return true;} // if it is not a return key or there is no web browsing history, keep the default // system behavior (this activity may be exited) return super. onKeyDown (keyCode, event) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. btn_nightmode: webView. loadUrl ("javascript: load_night ()"); break; case R. id. btn_lightmode: webView. loadUrl ("javascript: load_day ()"); break; default: break ;}}}

Http://download.csdn.net/detail/wwj_748/8554833

Reprinted Please note: IT_xiao xiaowu
Blog: http://blog.csdn.net/wwj_748
Mobile development enthusiasts: 299402133

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.