{"id":361,"date":"2023-02-25T08:15:58","date_gmt":"2023-02-25T00:15:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=361"},"modified":"2023-04-30T14:43:39","modified_gmt":"2023-04-30T06:43:39","slug":"android-soft-keyboard-display-hidden-event-listening","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-soft-keyboard-display-hidden-event-listening\/","title":{"rendered":"Android\u8f6f\u952e\u76d8\u663e\u793a\u9690\u85cf\u4e8b\u4ef6\u76d1\u542c"},"content":{"rendered":"<p>\u7531\u4e8eAndroid\u7cfb\u7edf\u5e76\u6ca1\u6709\u63d0\u4f9b\u8f6f\u952e\u76d8\u5f39\u5165\u548c\u5f39\u51fa\u7684\u7cfb\u7edf\u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u4eec\u6709\u65f6\u5019\u9700\u8981\u7528\u5230\u76d1\u542c\u8f6f\u952e\u76d8\u7684\u65f6\u5019\u4f1a\u975e\u5e38\u7684\u9ebb\u70e6\uff0c\u6839\u636e\u76d1\u542c\u89c6\u56fe\u6811\u4e2d\u5168\u5c40\u5e03\u5c40\u53d1\u751f\u6539\u53d8\u6216\u8005\u89c6\u56fe\u6811\u4e2d\u7684\u67d0\u4e2a\u89c6\u56fe\u7684\u53ef\u89c6\u72b6\u6001\u53d1\u751f\u6539\u53d8\u8d85\u8fc7\u4e00\u5b9a\u7684\u6570\u503c\u6765\u76d1\u542c\u8f6f\u952e\u76d8\u7684\u5f39\u5165\u5f39\u51fa\u4e8b\u4ef6\uff0c\u4f7f\u7528\u8d77\u6765\u4e5f\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<h3>\u81ea\u5b9a\u4e49\u76d1\u542c\u5de5\u5177\u7c7b<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class SoftKeyBoardListener {\n    private View rootView;\/\/Activity\u7684\u6839\u89c6\u56fe\n    int rootViewVisibleHeight;\/\/\u7eaa\u5f55\u6839\u89c6\u56fe\u7684\u663e\u793a\u9ad8\u5ea6\n    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;\n\n    public SoftKeyBoardListener(Activity activity) {\n        \/\/\u83b7\u53d6activity\u7684\u6839\u89c6\u56fe\n        rootView = activity.getWindow().getDecorView();\n        \/\/\u76d1\u542c\u89c6\u56fe\u6811\u4e2d\u5168\u5c40\u5e03\u5c40\u53d1\u751f\u6539\u53d8\u6216\u8005\u89c6\u56fe\u6811\u4e2d\u7684\u67d0\u4e2a\u89c6\u56fe\u7684\u53ef\u89c6\u72b6\u6001\u53d1\u751f\u6539\u53d8\n        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {\n            @Override\n            public void onGlobalLayout() {\n                \/\/\u83b7\u53d6\u5f53\u524d\u6839\u89c6\u56fe\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u7684\u5927\u5c0f\n                Rect r = new Rect();\n                rootView.getWindowVisibleDisplayFrame(r);\n                int visibleHeight = r.height();\n                Log.i(&quot;yezhou&quot;, &quot;&quot;+visibleHeight);\n                if (rootViewVisibleHeight == 0) {\n                    rootViewVisibleHeight = visibleHeight;\n                    return;\n                }\n\n                \/\/\u6839\u89c6\u56fe\u663e\u793a\u9ad8\u5ea6\u6ca1\u6709\u53d8\u5316\uff0c\u53ef\u4ee5\u770b\u4f5c\u8f6f\u952e\u76d8\u663e\u793a\uff0f\u9690\u85cf\u72b6\u6001\u6ca1\u6709\u6539\u53d8\n                if (rootViewVisibleHeight == visibleHeight) {\n                    return;\n                }\n\n                \/\/\u6839\u89c6\u56fe\u663e\u793a\u9ad8\u5ea6\u53d8\u5c0f\u8d85\u8fc7300\uff0c\u53ef\u4ee5\u770b\u4f5c\u8f6f\u952e\u76d8\u663e\u793a\u4e86\uff0c\u8be5\u6570\u503c\u53ef\u6839\u636e\u9700\u8981\u81ea\u884c\u8c03\u6574\n                if (rootViewVisibleHeight - visibleHeight &gt; 200) {\n                    if (onSoftKeyBoardChangeListener != null) {\n                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);\n                    }\n                    rootViewVisibleHeight = visibleHeight;\n                    return;\n                }\n\n                \/\/\u6839\u89c6\u56fe\u663e\u793a\u9ad8\u5ea6\u53d8\u5927\u8d85\u8fc7300\uff0c\u53ef\u4ee5\u770b\u4f5c\u8f6f\u952e\u76d8\u9690\u85cf\u4e86\uff0c\u8be5\u6570\u503c\u53ef\u6839\u636e\u9700\u8981\u81ea\u884c\u8c03\u6574\n                if (visibleHeight - rootViewVisibleHeight &gt; 200) {\n                    if (onSoftKeyBoardChangeListener != null) {\n                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);\n                    }\n                    rootViewVisibleHeight = visibleHeight;\n                    return;\n                }\n\n            }\n        });\n    }\n\n    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {\n        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;\n    }\n\n    public interface OnSoftKeyBoardChangeListener {\n        void keyBoardShow(int height);\n        void keyBoardHide(int height);\n    }\n\n    public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {\n        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);\n        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);\n    }\n}<\/code><\/pre>\n<h3>AndroidManifest.xml\u914d\u7f6e<\/h3>\n<p>\u6211\u4eec\u5728\u9700\u8981\u4f7f\u7528\u76d1\u542c\u7684Activity\u6e05\u5355\u6587\u4ef6\u4e2d\u8bbe\u7f6e<code>configChanges<\/code>\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;activity android:name=&quot;.MainActivity&quot;\n        android:launchMode=&quot;singleTop&quot;\n        android:configChanges=&quot;keyboardHidden&quot;\n        android:windowSoftInputMode=&quot;adjustPan|stateHidden&quot;\/&gt;<\/code><\/pre>\n<h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<p>\u6700\u540e\uff0c\u4f7f\u7528\u65b9\u6cd5\u4e5f\u975e\u5e38\u7684\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728Activity\u7c7b\u4e2d\u8c03\u7528\u65b9\u6cd5\u5373\u53ef\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u76d1\u542c\u8f6f\u4ef6\u76d8\u662f\u5426\u5f39\u8d77\nprivate void onKeyBoardListener() {\n    SoftKeyBoardListener.setListener(MainActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {\n        @Override\n        public void keyBoardShow(int height) {\n            Log.e(&quot;yezhou&quot;, &quot;\u952e\u76d8\u663e\u793a \u9ad8\u5ea6&quot; + height);\n        }\n\n        @Override\n        public void keyBoardHide(int height) {\n            Log.e(&quot;yezhou&quot;, &quot;\u952e\u76d8\u9690\u85cf \u9ad8\u5ea6&quot; + height);\n        }\n    });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8eAndroid\u7cfb\u7edf\u5e76\u6ca1\u6709\u63d0\u4f9b\u8f6f\u952e\u76d8\u5f39\u5165\u548c\u5f39\u51fa\u7684\u7cfb\u7edf\u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u4eec\u6709\u65f6\u5019\u9700\u8981\u7528\u5230\u76d1\u542c\u8f6f\u952e\u76d8\u7684\u65f6\u5019\u4f1a\u975e\u5e38\u7684\u9ebb\u70e6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[125],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-125"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}