{"id":166907,"date":"2024-05-08T15:51:57","date_gmt":"2024-05-08T07:51:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166907.html"},"modified":"2024-05-08T15:52:05","modified_gmt":"2024-05-08T07:52:05","slug":"javascript%e8%83%bd%e5%90%a6%e8%b0%83%e7%94%a8%e5%ae%89%e5%8d%93%e7%9a%84%e8%bd%af%e9%94%ae%e7%9b%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/166907.html","title":{"rendered":"javascript\u80fd\u5426\u8c03\u7528\u5b89\u5353\u7684\u8f6f\u952e\u76d8"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234259\/e0d1a51a-52c0-4507-9d97-f87b172c0df1.webp\" alt=\"javascript\u80fd\u5426\u8c03\u7528\u5b89\u5353\u7684\u8f6f\u952e\u76d8\" \/><\/p>\n<p><p>JavaScript\u53ef\u4ee5\u8c03\u7528\u5b89\u5353\u7684\u8f6f\u952e\u76d8\uff0c\u4e3b\u8981\u901a\u8fc7\u4e09\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a<strong>\u501f\u52a9WebView\u63a7\u4ef6\u3001\u4f7f\u7528JavaScriptInterface\u3001\u901a\u8fc7Focus\u4e8b\u4ef6<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u8ba9\u7528\u6237\u4ea4\u4e91\u7684\u754c\u9762\u6216\u4e8b\u4ef6\uff0c\u8fdb\u800c\u89e6\u53d1\u5b89\u5353\u7cfb\u7edf\u9ed8\u8ba4\u7684\u8f6f\u952e\u76d8\u3002<strong>\u501f\u52a9WebView\u63a7\u4ef6<\/strong>\u7684\u65b9\u6cd5\u7279\u522b\u503c\u5f97\u6ce8\u610f\uff0c\u56e0\u4e3a\u5b83\u4e3aJavaScript\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8fd0\u884c\u5728\u5b89\u5353\u5e94\u7528\u4e2d\u7684\u6865\u6881\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5b89\u5353\u5e94\u7528\u5185\u5d4c\u5957\u7f51\u9875\uff0c\u5e76\u901a\u8fc7WebView\u7684\u5404\u79cd\u8bbe\u7f6e\u548c\u4f18\u5316\uff0c\u6765\u5b9e\u73b0\u66f4\u6d41\u7545\u7684\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u501f\u52a9WEBVIEW\u63a7\u4ef6<\/h2>\n<\/p>\n<p><p>\u5b89\u5353\u7684WebView\u63a7\u4ef6\u80fd\u591f\u8ba9\u4f60\u5728\u5b89\u5353\u5e94\u7528\u4e2d\u76f4\u63a5\u5c55\u793a\u7f51\u9875\uff0c\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97JavaScript\u53ef\u4ee5\u5728\u5b89\u5353\u73af\u5883\u4e0b\u8fd0\u884c\u3002\u8981\u8c03\u7528\u8f6f\u952e\u76d8\uff0c\u901a\u5e38\u505a\u6cd5\u662f\u5728WebView\u52a0\u8f7d\u7684\u7f51\u9875\u4e2d\u653e\u7f6e\u4e00\u4e2a\u6587\u672c\u8f93\u5165\u6846\u6216<textarea>\u3002\u5f53\u7528\u6237\u70b9\u51fb\u8be5\u8f93\u5165\u6846\u65f6\uff0c\u5b89\u5353\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5f39\u51fa\u8f6f\u952e\u76d8\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u5b89\u5353\u5e94\u7528\u7684\u754c\u9762\u5e03\u5c40\uff08XML\u6587\u4ef6\uff09\u4e2d\u52a0\u5165WebView\u63a7\u4ef6\u3002\u7136\u540e\u5728\u5b89\u5353\u7684Activity\u4e2d\u521d\u59cb\u5316\u8be5WebView\uff0c\u5e76\u8f7d\u5165\u542b\u6709\u8f93\u5165\u6846\u7684HTML\u9875\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">WebView webView = (WebView) findViewById(R.id.webview);<\/p>\n<p>webView.getSettings().setJavaScriptEnabled(true); \/\/ \u5f00\u542fJavaScript\u652f\u6301<\/p>\n<p>webView.loadUrl(&quot;file:\/\/\/android_asset\/my_page.html&quot;); \/\/ \u8f7d\u5165\u672c\u5730assets\u6587\u4ef6\u5939\u4e0b\u7684\u9875\u9762<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>my_page.html<\/code>\u5e94\u8be5\u662f\u4e00\u4e2a\u5305\u542b\u6587\u672c\u8f93\u5165\u7684HTML\u6587\u4ef6\u3002\u7528\u6237\u5728WebView\u4e2d\u70b9\u51fb\u8f93\u5165\u6846\u65f6\uff0c\u5b89\u5353\u7cfb\u7edf\u4f1a\u81ea\u52a8\u663e\u793a\u8f6f\u952e\u76d8\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528JAVASCRIPTINTERFACE<\/h2>\n<\/p>\n<p><p>JavaScriptInterface\u662f\u5b89\u5353\u63d0\u4f9b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u8ba9JavaScript\u4ee3\u7801\u8c03\u7528\u5b89\u5353\uff08Java\/Kotlin\uff09\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u542b\u6709\u7279\u5b9a\u65b9\u6cd5\u7684Java\/Kotlin\u7c7b\uff0c\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u5904\u7406\u8f6f\u952e\u76d8\u7684\u663e\u793a\u548c\u9690\u85cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class WebAppInterface {<\/p>\n<p>    Context mContext;<\/p>\n<p>    \/\/ \u5b9e\u4f8b\u5316\u63a5\u53e3\u5e76\u8bbe\u7f6e\u4e0a\u4e0b\u6587<\/p>\n<p>    WebAppInterface(Context c) {<\/p>\n<p>        mContext = c;<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u4f9bJavaScript\u8c03\u7528\u7684\u65b9\u6cd5\u6765\u663e\u793a\u8f6f\u952e\u76d8<\/p>\n<p>    @JavascriptInterface<\/p>\n<p>    public void showKeyboard() {<\/p>\n<p>        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);<\/p>\n<p>        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>\/\/ \u5728\u4f60\u7684WebView\u8bbe\u7f6e\u4e2d\u6dfb\u52a0\u8fd9\u4e2a\u63a5\u53e3<\/p>\n<p>webView.addJavascriptInterface(new WebAppInterface(this), &quot;Android&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728HTML\u7684JavaScript\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>Android.showKeyboard()<\/code>\u6765\u663e\u793a\u8f6f\u952e\u76d8\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7FOCUS\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u8fd9\u662f\u4e00\u79cd\u8f83\u4e3a\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u4e8e\u4e0d\u9700\u8981\u4e0e\u5b89\u5353\u539f\u751f\u4ee3\u7801\u4ea4\u4e92\u7684\u573a\u666f\u3002\u5f53\u4f60\u60f3\u8981\u5728\u7528\u6237\u70b9\u51fb\u67d0\u4e2a\u5143\u7d20\u65f6\u5f39\u51fa\u8f6f\u952e\u76d8\uff0c\u53ef\u4ee5\u5728\u8be5\u5143\u7d20\u4e0a\u8bbe\u7f6e<code>focus<\/code>\u4e8b\u4ef6\uff0c\u5e76\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528<code>element.focus()<\/code>\u547d\u4ee4\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5524\u8d77\u8f6f\u952e\u76d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;input id=&quot;my-input&quot; type=&quot;text&quot; onclick=&quot;this.focus();&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u7b80\u5355\u6613\u884c\uff0c\u4e0d\u9700\u8981\u7f16\u5199\u989d\u5916\u7684\u5b89\u5353\u4ee3\u7801\u3002\u4f46\u5b83\u7684\u6548\u679c\u53ef\u80fd\u4f1a\u53d7\u5230\u5b89\u5353\u8bbe\u5907\u548c\u7248\u672c\u7684\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u8c03\u7528\u5b89\u5353\u8f6f\u952e\u76d8\u5728\u6280\u672f\u4e0a\u53ef\u884c\uff0c\u4f46\u4ece\u7528\u6237\u4f53\u9a8c\u89d2\u5ea6\u51fa\u53d1\uff0c\u6211\u4eec\u4e0d\u63a8\u8350\u5728\u7f51\u9875\u5e94\u7528\u4e2d\u8fc7\u4e8e\u9891\u7e41\u5730\u63a7\u5236\u8f6f\u952e\u76d8\u3002\u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u662f\u8ba9\u7cfb\u7edf\u6839\u636e\u7528\u6237\u7684\u64cd\u4f5c\u81ea\u7136\u5730\u5f39\u51fa\u548c\u9690\u85cf\u8f6f\u952e\u76d8\u3002\u5982\u679c\u786e\u5b9e\u9700\u8981\u5728JavaScript\u4e2d\u63a7\u5236\u8f6f\u952e\u76d8\uff0c\u5c3d\u91cf\u91c7\u7528\u4e0a\u8ff0\u63d0\u5230\u7684<strong>\u501f\u52a9WebView\u63a7\u4ef6\u6216\u4f7f\u7528JavaScriptInterface<\/strong>\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u8f83\u597d\u7684\u7528\u6237\u4f53\u9a8c\u548c\u8f83\u9ad8\u7684\u53ef\u63a7\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5efa\u7acb\u4e00\u4e2a\u6d41\u7545\u3001\u76f4\u89c2\u7684\u7528\u6237\u8f93\u5165\u6d41\u7a0b\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u5e94\u7528\u7684\u6574\u4f53\u4f53\u9a8c\u3002\u5f00\u53d1\u8005\u5728\u8bbe\u8ba1\u754c\u9762\u65f6\uff0c\u5e94\u5f53\u8003\u8651\u5230\u8f6f\u952e\u76d8\u7684\u5f39\u51fa\u548c\u9690\u85cf\u5bf9\u5e03\u5c40\u7684\u5f71\u54cd\uff0c\u4f8b\u5982\u907f\u514d\u8f93\u5165\u6846\u88ab\u8f6f\u952e\u76d8\u906e\u6321\u7684\u95ee\u9898\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f18\u5316WebView\u7684\u52a0\u8f7d\u901f\u5ea6\u548c\u6027\u80fd\uff0c\u786e\u4fdd\u7528\u6237\u5728\u8f93\u5165\u65f6\u80fd\u591f\u83b7\u5f97\u53ca\u65f6\u7684\u53cd\u9988\uff0c\u4e5f\u662f\u4e0d\u53ef\u5ffd\u89c6\u7684\u4e00\u73af\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528JavaScript\u8c03\u7528\u5b89\u5353\u7684\u8f6f\u952e\u76d8\uff1f<\/strong><br \/>\n\u8c03\u7528\u5b89\u5353\u8f6f\u952e\u76d8\u662f\u901a\u8fc7\u4f7f\u7528JavaScript\u4e2d\u7684<code>focus()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\u3002\u60a8\u53ef\u4ee5\u5c06<code>focus()<\/code>\u65b9\u6cd5\u5e94\u7528\u4e8e\u5177\u6709\u53ef\u7f16\u8f91\u529f\u80fd\u7684\u8f93\u5165\u5b57\u6bb5\uff0c\u4ee5\u89e6\u53d1\u5b89\u5353\u8bbe\u5907\u4e0a\u7684\u8f6f\u952e\u76d8\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">document.getElementById(&quot;inputField&quot;).focus();\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c\u201cinputField\u201d\u662f\u60a8\u8981\u89e6\u53d1\u8f6f\u952e\u76d8\u7684\u8f93\u5165\u5b57\u6bb5\u7684ID\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u9875\u9762\u52a0\u8f7d\u65f6\uff0c\u5b89\u5353\u8bbe\u5907\u7684\u8f6f\u952e\u76d8\u5c06\u81ea\u52a8\u5f39\u51fa\uff0c\u4ee5\u4fbf\u7528\u6237\u8fdb\u884c\u8f93\u5165\u3002<\/p>\n<p><strong>2. \u662f\u5426\u53ef\u4ee5\u901a\u8fc7JavaScript\u5173\u95ed\u5b89\u5353\u7684\u8f6f\u952e\u76d8\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u5173\u95ed\u5b89\u5353\u8bbe\u5907\u4e0a\u7684\u8f6f\u952e\u76d8\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>blur()<\/code>\u65b9\u6cd5\u5c06\u7126\u70b9\u4ece\u5f53\u524d\u8f93\u5165\u5b57\u6bb5\u4e2d\u79fb\u5f00\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">document.getElementById(&quot;inputField&quot;).blur();\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4f7f\u8f6f\u952e\u76d8\u6d88\u5931\uff0c\u5e76\u4e14\u7528\u6237\u53ef\u4ee5\u7ee7\u7eed\u6d4f\u89c8\u9875\u9762\u800c\u4e0d\u5fc5\u952e\u5165\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p><strong>3. \u6211\u60f3\u5728\u5b89\u5353\u8bbe\u5907\u4e0a\u663e\u793a\u81ea\u5b9a\u4e49\u7684\u952e\u76d8\uff0c\u662f\u5426\u53ef\u884c\uff1f<\/strong><br \/>\n\u5f88\u62b1\u6b49\uff0cJavaScript\u672c\u8eab\u5e76\u4e0d\u80fd\u76f4\u63a5\u663e\u793a\u81ea\u5b9a\u4e49\u952e\u76d8\u3002\u5b89\u5353\u8bbe\u5907\u4e0a\u7684\u8f6f\u952e\u76d8\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u63a7\u5236\u7684\uff0c\u4e0d\u5141\u8bb8\u4f7f\u7528JavaScript\u6216\u5176\u4ed6\u524d\u7aef\u6280\u672f\u8fdb\u884c\u5b9a\u5236\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u501f\u52a9CSS\u548cHTML\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u8f93\u5165\u754c\u9762\uff0c\u4ee5\u6a21\u62df\u81ea\u5b9a\u4e49\u952e\u76d8\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002\u8fd9\u6d89\u53ca\u5230\u8bbe\u8ba1\u548c\u5f00\u53d1\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7528\u6237\u754c\u9762\uff0c\u53ef\u4ee5\u4e0e\u5b89\u5353\u8bbe\u5907\u4e0a\u7684\u8f6f\u952e\u76d8\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u53ef\u4ee5\u8c03\u7528\u5b89\u5353\u7684\u8f6f\u952e\u76d8\uff0c\u4e3b\u8981\u901a\u8fc7\u4e09\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a\u501f\u52a9WebView\u63a7\u4ef6\u3001\u4f7f\u7528JavaScrip [&hellip;]","protected":false},"author":3,"featured_media":166928,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166907"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=166907"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166907\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166928"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}