{"id":1154,"date":"2012-04-01T16:48:15","date_gmt":"2012-04-01T08:48:15","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1154"},"modified":"2012-04-01T16:55:25","modified_gmt":"2012-04-01T08:55:25","slug":"androidhello-webview","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1154","title":{"rendered":"[Android]Hello WebView"},"content":{"rendered":"<p>\u672c\u4f86\u4e00\u76f4\u662f\u60f3\u5bebAndroid\uff0c\u4f46\u5be6\u969b\u5de5\u4f5c\u5f8c\u537b\u6709\u5f88\u591a\u7684\u4efb\u52d9\u8b93\u6211\u7121\u6cd5\u9019\u9ebc\u5feb\u78b0\u5230\u6211\u60f3\u8981\u5beb\u7684Android<\/p>\n<p>\u525b\u597d\u9019\u5468\u672b\u6c92\u5b89\u6392\u4ec0\u9ebc\u6d3b\u52d5\uff0c\u4e5f\u6c92\u4ec0\u9ebc\u4e8b\u60c5\u53ef\u505a\uff0c\u5c31\u4f86\u81ea\u5df1\u5beb\u4e00\u500bHello Android<\/p>\n<p>\u524d\u93ae\u5b50\u56e0\u70ba\u5de5\u4f5c\u4efb\u52d9\u9700\u8981\uff0c\u6240\u4ee5\u7814\u7a76\u4e86Phone7(\u56db\u6708\u5e95\u4ee5\u524d\u61c9\u8a72\u5c31\u6703\u5728\u5e02\u5834\u4e0a\u7dda\u4e86)<\/p>\n<p>\u7814\u7a76\u540c\u6642\u5beb\u4e86\u4e00\u7bc7<a href=\"http:\/\/learn-house.idv.tw\/?p=994\" target=\"_blank\">Microsoft.Phone.Controls.WebBrowser\u61c9\u7528<\/a><\/p>\n<p>\u6240\u4ee5\u540c\u6a23\u7684Android\u4e5f\u6703\u6709\u540c\u6a23\u7684\u61c9\u7528\uff0c\u6240\u4ee5\u5c31\u628a\u5979\u4f86\u7576\u4f5c\u7b2c\u4e00\u6b21\u5beb\u7a0b\u5f0f\u7684Hello Android<\/p>\n<p><!--more-->\u525b\u597d\u7d50\u5408\u524d\u9663\u5b50\u770b\u5b8c\u4e00\u672c\u6295\u8cc7\u7406\u8ca1\u7684\u96dc\u8a8c\uff0c\u7a81\u767c\u5947\u60f3\u7d50\u5408yahoo\u7684\u80a1\u7968\u7cfb\u7d71\uff0c<a href=\"http:\/\/learn-house.idv.tw\/?page_id=1140\" target=\"_blank\">\u81ea\u52d5\u7b97\u51fa\u6b96\u5229\u7387<\/a>\uff0c\u8b93Android\u4e5f\u80fd\u900f\u904e\u700f\u89bd\u5668\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u9019\u88e1\u6211\u5c31\u4e0d\u589c\u8ff0\u600e\u9ebc\u5b89\u88ddIDE\u8207SDK\uff0c\u9019\u65b9\u9762\u7db2\u8def\u6587\u7ae0\u61c9\u8a72\u591a\u4e0d\u52dd\u6578\uff0c\u9019\u88e1\u5c31\u76f4\u63a5\u958b\u59cb\u8aaa\u600e\u9ebc\u5bebHello WebVew<\/p>\n<p>\u9996\u5148\u4e00\u958b\u59cb\uff0c\u5728\u4f7f\u7528\u9019\u500b\u985e\u5225\u6642\u8981\u5148\u5728AndroidManifest.xml\u52a0\u5165&lt;uses-permission android:name=&#8221;android.permission.INTERNET&#8221;\/&gt;\u4f86\u5141\u8a31\u8edf\u9ad4\u64da\u6709\u5b58\u53d6\u7db2\u8def\u7684\u6b0a\u9650<\/p>\n<p>[html]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n package=&quot;tw.com.learnhouse&quot;<br \/>\n android:versionCode=&quot;1&quot;<br \/>\n android:versionName=&quot;1.0&quot; &gt;<\/p>\n<p> &lt;uses-sdk android:minSdkVersion=&quot;15&quot; \/&gt;<br \/>\n &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;\/&gt;<\/p>\n<p> &lt;application<br \/>\n android:icon=&quot;@drawable\/ic_launcher&quot;<br \/>\n android:label=&quot;@string\/app_name&quot; &gt;<br \/>\n &lt;activity<br \/>\n android:name=&quot;.StockActivity&quot;<br \/>\n android:label=&quot;@string\/app_name&quot; &gt;<br \/>\n &lt;intent-filter&gt;<br \/>\n &lt;action android:name=&quot;android.intent.action.MAIN&quot; \/&gt;<\/p>\n<p> &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; \/&gt;<br \/>\n &lt;\/intent-filter&gt;<br \/>\n &lt;\/activity&gt;<br \/>\n &lt;\/application&gt;<\/p>\n<p>&lt;\/manifest&gt;[\/html]<\/p>\n<p>\u5982\u679c\u6c92\u52a0\u90a3\u4e00\u6bb5\u7684\u8a71\uff0c\u600e\u6a23\u90fd\u7121\u6cd5\u9023\u5230\u7db2\u969b\u7db2\u8def\u800c\u51fa\u73fe\u5982\u4e0b\u756b\u9762<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"Android\" src=\"https:\/\/lh3.googleusercontent.com\/-cModVbks9EU\/T3gMzoqc3hI\/AAAAAAAAEBE\/a4W2Naz5BCI\/s480\/device-2012-04-01-151454.png\" alt=\"\" width=\"286\" height=\"430\" \/><\/p>\n<p>\u518d\u4f86\u5c31\u662f\u5728Layout\u4e2d\u52a0\u5165WebView\uff0c\u6253\u958bmain.xml\u4e26\u52a0\u5165WebView layout\u76f8\u95dc\u6558\u8ff0<\/p>\n<p>[html]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n android:layout_width=&quot;fill_parent&quot;<br \/>\n android:layout_height=&quot;fill_parent&quot;<br \/>\n android:orientation=&quot;vertical&quot; &gt;<\/p>\n<p> &lt;TextView<br \/>\n android:layout_width=&quot;fill_parent&quot;<br \/>\n android:layout_height=&quot;wrap_content&quot;<br \/>\n android:text=&quot;@string\/hello&quot; \/&gt;<\/p>\n<p> &lt;WebView<br \/>\n android:id=&quot;@+id\/webStockView&quot;<br \/>\n android:layout_width=&quot;match_parent&quot;<br \/>\n android:layout_height=&quot;match_parent&quot; \/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;[\/html]<\/p>\n<p>\u4e4b\u5f8c\u5c31\u5269\u7a0b\u5f0f\u78bc\u7684\u5be6\u4f5c\u4e86\uff1a<\/p>\n<p>[java]package tw.com.learnhouse;<\/p>\n<p>import android.app.Activity;<br \/>\nimport android.os.Bundle;<br \/>\nimport android.webkit.*;<\/p>\n<p>public class StockActivity extends Activity {<br \/>\n \/** Called when the activity is first created. *\/<br \/>\n @Override<br \/>\n public void onCreate(Bundle savedInstanceState) {<br \/>\n super.onCreate(savedInstanceState);<br \/>\n setContentView(R.layout.main);<\/p>\n<p> String stockURL = &quot;http:\/\/learn-house.idv.tw\/?page_id=1140&quot;;<br \/>\n WebView stockBrowser = (WebView)findViewById(R.id.webStockView);<\/p>\n<p> WebSettings websettings = stockBrowser.getSettings();<br \/>\n websettings.setSupportZoom(true);<br \/>\n websettings.setBuiltInZoomControls(true);<br \/>\n websettings.setJavaScriptEnabled(true);<\/p>\n<p> stockBrowser.setWebViewClient(new WebViewClient());<br \/>\n \/\/stockBrowser.setWebChromeClient(new WebChromeClient());<br \/>\n stockBrowser.loadUrl(stockURL);<br \/>\n }<br \/>\n}[\/java]<\/p>\n<p>\u5728\u9019\u88e1\u8981\u7279\u5225\u7684\u63d0\u9192\u5beb\u7a0b\u5f0f\u7684\u4eba\u54e1\uff0c\u5728\u5beb\u4efb\u4f55\u7a0b\u5f0f\u90fd\u8981\u5b9a\u7fa9\u81ea\u5df1\u7684package\u540d\u7a31<\/p>\n<p>\u9019\u662f\u70ba\u4e86\u9632\u6b62\u4e00\u500b\u5927\u5c08\u6848\u88e1\u6216\u8005\u4e0d\u540c\u5ee0\u5546\u6240\u4e00\u8d77\u64b0\u5beb\u7684\u5c08\u6848\uff0c\u500b\u5225\u5ee0\u5546\u5728\u985e\u5225\u547d\u540d\u6642\u6709\u540c\u6a23\u7684\u985e\u5225\u540d\u7a31\u767c\u751f<\/p>\n<p>\u9019\u4e5f\u662f\u6211\u5728\u5bebPhone7\u7684dll\u7d66\u5176\u4ed6\u5ee0\u5546\u4f7f\u7528\u6642\u767c\u73fe\uff0c\u539f\u4f86namespace(\u5c31\u662fjava\u7684package)\u662f\u9019\u9ebc\u7684\u91cd\u8981<\/p>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u61c9\u8a72\u4e0d\u7528\u591a\u8aaa\u660e\u5c31\u80fd\u731c\u5f97\u51fa\u4f86\u6bcf\u884c\u4ee3\u8868\u7684\u7528\u610f<\/p>\n<p>\u4e00\u958b\u59cb\u5148\u4f7f\u7528WebView\u985e\u5225\u4f86\u53d6\u5f97\u6211\u5011\u5728layout\u8a2d\u5b9a\u7684\u700f\u89bd\u5668\u8b58\u5225id<\/p>\n<p>\u518d\u4f86\u7528WebSettings\u985e\u5225\u4f86\u505a\u700f\u89bd\u5668\u7684\u4e00\u4e9b\u8a2d\u5b9a<\/p>\n<p>[java]websettings.setSupportZoom(true);\/\/\u652f\u63f4Zoom In\/Out<br \/>\n websettings.setBuiltInZoomControls(true);\/\/\u6efe\u52d5\u6642\u986f\u793a\u7e2e\u653e\u7684+\/-\u63a7\u5236<br \/>\n websettings.setJavaScriptEnabled(true);\/\/\u652f\u63f4JavaScript[\/java]<\/p>\n<p>\u6700\u5f8c\u52a0\u5165stockBrowser.loadUrl(stockURL);\u5c31\u80fd\u555f\u52d5\u700f\u89bd\u5668\u4e26\u8f09\u5165\u6307\u5b9a\u7684URL\u7db2\u9801<\/p>\n<p>\u958b\u555f\u7db2\u9801\u5f8c\u5982\u679c\u4f60\u60f3\u7e7c\u7e8c\u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u700f\u89bd\u5668\uff0c\u5c31\u5fc5\u9808\u518d\u52a0\u5165stockBrowser.setWebViewClient(new WebViewClient());<\/p>\n<p>\u800c\u5728\u547c\u53eb\u7684\u6642\u5019\u6703\u767c\u73fe\u6709setWebViewClient\u8207setWebChromeClient<\/p>\n<p>\u5169\u500b\u5dee\u5225\u5728\u65bcsetWebChromeClient\u6709\u6bd4\u8f03\u591a\u7684\u652f\u63f4\uff0c\u53ef\u4ee5\u7528\u4f86\u8655\u7406\u6bd4\u8f03\u8c50\u5bcc\u7684\u7db2\u9801\u6548\u679c\u8207JS\u7684\u4f7f\u7528<\/p>\n<p>\u4f46\u4e00\u822c\u7684\u7db2\u9801WebViewClient\u5c31\u5f88\u5920\u4f7f\u7528\u4e86\u3002\u6700\u5f8c\u6210\u54c1\u5716\u5c31\u5982\u4e0b\u756b\u9762\u56c9!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"android\" src=\"https:\/\/lh4.googleusercontent.com\/-OlMQiO0b5I8\/T3gMznXVpqI\/AAAAAAAAEBI\/3Hzrb6Ybv0Q\/s480\/device-2012-04-01-152235.png\" alt=\"\" width=\"286\" height=\"430\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u4f86\u4e00\u76f4\u662f\u60f3\u5bebAndroid\uff0c\u4f46\u5be6\u969b\u5de5\u4f5c\u5f8c\u537b\u6709\u5f88\u591a\u7684\u4efb\u52d9\u8b93\u6211\u7121\u6cd5\u9019\u9ebc\u5feb\u78b0\u5230\u6211\u60f3\u8981\u5beb\u7684Android \u525b\u597d\u9019\u5468\u672b<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=1154\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1154","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1154"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1154\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}