{"id":1286,"date":"2023-03-18T10:12:47","date_gmt":"2023-03-18T02:12:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1286"},"modified":"2023-04-29T09:20:20","modified_gmt":"2023-04-29T01:20:20","slug":"android-elegantly-handle-button-repeated-clicks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-elegantly-handle-button-repeated-clicks\/","title":{"rendered":"Android\u4f18\u96c5\u5730\u5904\u7406\u6309\u94ae\u91cd\u590d\u70b9\u51fb"},"content":{"rendered":"<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.jianshu.com\/p\/7b354eb8d0d3\">https:\/\/www.jianshu.com\/p\/7b354eb8d0d3<\/a><\/p>\n<h2>\u4f20\u7edf\u5904\u7406\u65b9\u5f0f<\/h2>\n<p>\uff081\uff09\u6bcf\u4e2a\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4e2d\uff0c\u8bb0\u5f55\u70b9\u51fb\u65f6\u95f4\uff0c\u5224\u65ad\u662f\u5426\u8d85\u8fc7\u70b9\u51fb\u65f6\u95f4\u95f4\u9694<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">private long mLastClickTime = 0;\npublic static final long TIME_INTERVAL = 1000L;\nprivate Button btTest;\nprivate void initView() {\n    btTest = findViewById(R.id.bt_test);\n    btTest.setOnClickListener(new View.OnClickListener() {\n        @Override\n        public void onClick(View v) {\n            long nowTime = System.currentTimeMillis();\n            if (nowTime - mLastClickTime &gt; TIME_INTERVAL) {\n                \/\/ do something\n                mLastClickTime = nowTime;\n            } else {\n                Toast.makeText(MainActivity.this, &quot;\u4e0d\u8981\u91cd\u590d\u70b9\u51fb&quot;, Toast.LENGTH_SHORT).show();\n            }\n        }\n    });\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6bcf\u4e2a\u70b9\u51fb\u4e8b\u4ef6\u90fd\u9700\u8981\u5199\u4e00\u4e2a\u65f6\u95f4\u5224\u65ad\uff0c\u91cd\u590d\u4ee3\u7801\u5f88\u591a\u3002<\/p>\n<p>\uff082\uff09\u5c01\u88c5\u4e00\u4e2a\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5904\u7406\u70b9\u51fb\u95f4\u9694\u5224\u65ad<\/p>\n<pre><code class=\"language-java\">public abstract class CustomClickListener implements View.OnClickListener {\n    private long mLastClickTime;\n    private long timeInterval = 1000L;\n\n    public CustomClickListener() {\n\n    }\n\n    public CustomClickListener(long interval) {\n        this.timeInterval = interval;\n    }\n\n    @Override\n    public void onClick(View v) {\n        long nowTime = System.currentTimeMillis();\n        if (nowTime - mLastClickTime &gt; timeInterval) {\n            \/\/ \u5355\u6b21\u70b9\u51fb\u4e8b\u4ef6\n            onSingleClick();\n            mLastClickTime = nowTime;\n        } else {\n            \/\/ \u5feb\u901f\u70b9\u51fb\u4e8b\u4ef6\n            onFastClick();\n        }\n    }\n\n    protected abstract void onSingleClick();\n    protected abstract void onFastClick();\n}<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">btTest.setOnClickListener(new CustomClickListener() {\n    @Override\n    protected void onSingleClick() {\n        Log.d(&quot;xxx&quot;, &quot;onSingleClick&quot;);\n    }\n\n    @Override\n    protected void onFastClick() {\n        Log.d(&quot;xxx&quot;, &quot;onFastClick&quot;);\n    }\n});<\/code><\/pre>\n<p>\u76f8\u6bd4\u4e8e\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5c06\u91cd\u590d\u70b9\u51fb\u7684\u5224\u65ad\u5c01\u88c5\u5728CustomClickListener\u5185\u90e8\uff0c\u5916\u90e8\u65e0\u9700\u5904\u7406\u65f6\u95f4\u5224\u65ad\uff0c\u53ea\u9700\u8981\u5b9e\u73b0\u70b9\u51fb\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<p>\uff083\uff09\u5229\u7528RxAndroid\u5904\u7406\u91cd\u590d\u70b9\u51fb<\/p>\n<pre><code class=\"language-java\">RxView.clicks(view)\n    .throttleFirst(1, TimeUnit.SECONDS)\n    .subscribe(new Consumer&lt;Object&gt;() {\n        @Override\n        public void accept(Object o) throws Exception {\n            \/\/ do something\n        }\n     });<\/code><\/pre>\n<p>\u54cd\u5e94\u5f0f\u5730\u5904\u7406\u6309\u94ae\u70b9\u51fb\uff0c\u5229\u7528rxjava\u7684\u64cd\u4f5c\u7b26\uff0c\u6765\u9632\u6b62\u91cd\u590d\u70b9\u51fb\uff0c\u76f8\u8f83\u4e8e\u7b2c1,2\u65b9\u6848\u6765\u8bf4\uff0c\u6b64\u65b9\u6cd5\u66f4\u4e3a\u4f18\u96c5\u4e00\u4e9b\u3002<\/p>\n<p>\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u4e0d\u8bba\u54ea\u4e00\u79cd\uff0c\u90fd\u5bf9\u539f\u6709\u70b9\u51fb\u4e8b\u4ef6\u6709\u5f88\u5927\u7684\u4fb5\u5165\u6027\uff0c\u8981\u4e48\u9700\u8981\u5f80Click\u4e8b\u4ef6\u4e2d\u52a0\u65b9\u6cd5\uff0c\u8981\u4e48\u9700\u8981\u66ff\u6362\u6574\u4e2aClick\u4e8b\u4ef6\uff0c\u90a3\u4e48\uff0c\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u6539\u52a8\u539f\u6709\u903b\u8f91\u7684\u60c5\u51b5\u4e0b\uff0c\u53c8\u80fd\u5f88\u597d\u5730\u5904\u7406\u6309\u94ae\u7684\u91cd\u590d\u70b9\u51fb\u5462\uff1f<\/p>\n<h2>\u66f4\u4e3a\u4f18\u96c5\u7684AOP\u5904\u7406\u65b9\u5f0f<\/h2>\n<p>\u5f80\u540c\u4e00\u7c7b\u578b\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u90fd\u52a0\u4e0a\u7edf\u4e00\u7684\u5904\u7406\u903b\u8f91\uff0c\u6211\u4eec\u5f88\u5feb\u5c31\u80fd\u60f3\u5230\u4e00\u4e2a\u8bcd\uff1aAOP\uff0c\u6ca1\u9519\uff0c\u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528AOP\u6765\u89e3\u51b3\u91cd\u590d\u70b9\u51fb\u95ee\u9898\uff1f<\/p>\n<h3>\u5f15\u5165Aspectj<\/h3>\n<p>Android \u4e0a\u4f7f\u7528AOP\u7f16\u7a0b\uff0c\u4e00\u822c\u4f7f\u7528Aspectj\u8fd9\u4e2a\u5e93<\/p>\n<p>\u7ad9\u5728\u5de8\u4eba\u7684\u80a9\u8180\u4e0a\uff0c\u6caa\u6c5f\u5df2\u7ecf\u5f00\u6e90\u4e86Aspectj\u7684Gradle\u63d2\u4ef6\uff0c\u65b9\u4fbf\u6211\u4eec\u4f7f\u7528Aspectj<\/p>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684build.gradle\u4e2d\uff0c\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n<pre><code>dependencies {\n    ......\n    classpath &#039;com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8&#039;\n}<\/code><\/pre>\n<p>\u5728app\u6216\u5176\u4ed6module\u76ee\u5f55\u4e0b\u7684build.gradle\u4e2d\uff0c\u6dfb\u52a0\uff1a<\/p>\n<pre><code>\/\/ \u6ce8\u610f\uff1a\u4e3bApp\u4e2d\u8bf7\u786e\u4fdd\u6dfb\u52a0aspectjx\napply plugin: &#039;android-aspectjx&#039;\n\ndependencies {\n    ......\n    implementation &#039;org.aspectj:aspectjrt:1.9.4&#039;\n}<\/code><\/pre>\n<blockquote>\n<p>\u6dfb\u52a0<code>@SingleClick<\/code>\u6ce8\u89e3\u7684module\u5747\u9700\u5f15\u5165\u63d2\u4ef6<code>apply plugin: &#039;android-aspectjx&#039;<\/code><\/p>\n<\/blockquote>\n<h3>\u6dfb\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6ce8\u89e3<\/h3>\n<pre><code class=\"language-java\">@Retention(RetentionPolicy.RUNTIME)\n@Target(ElementType.METHOD)\npublic @interface SingleClick {\n    \/* \u70b9\u51fb\u95f4\u9694\u65f6\u95f4 *\/\n    long value() default 1000;\n}<\/code><\/pre>\n<p>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6ce8\u89e3\u7684\u539f\u56e0\u662f\uff0c\u65b9\u4fbf\u7ba1\u7406\u54ea\u4e9b\u65b9\u6cd5\u4f7f\u7528\u4e86\u91cd\u590d\u70b9\u51fb\u7684AOP\uff0c\u540c\u65f6\u53ef\u4ee5\u5728\u6ce8\u89e3\u4e2d\u4f20\u5165\u70b9\u51fb\u65f6\u95f4\u95f4\u9694\uff0c\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<h3>\u5c01\u88c5\u4e00\u4e2a\u91cd\u590d\u70b9\u51fb\u5224\u65ad\u5de5\u5177\u7c7b<\/h3>\n<pre><code class=\"language-java\">public final class ClickUtil {\n\n    \/**\n     * \u6700\u8fd1\u4e00\u6b21\u70b9\u51fb\u7684\u65f6\u95f4\n     *\/\n    private static long mLastClickTime;\n    \/**\n     * \u6700\u8fd1\u4e00\u6b21\u70b9\u51fb\u7684\u63a7\u4ef6ID\n     *\/\n    private static int mLastClickViewId;\n\n    \/**\n     * \u662f\u5426\u662f\u5feb\u901f\u70b9\u51fb\n     *\n     * @param v  \u70b9\u51fb\u7684\u63a7\u4ef6\n     * @param intervalMillis  \u65f6\u95f4\u95f4\u671f\uff08\u6beb\u79d2\uff09\n     * @return  true:\u662f\uff0cfalse:\u4e0d\u662f\n     *\/\n    public static boolean isFastDoubleClick(View v, long intervalMillis) {\n        int viewId = v.getId();\n        long time = System.currentTimeMillis();\n        long timeInterval = Math.abs(time - mLastClickTime);\n        if (timeInterval &lt; intervalMillis &amp;&amp; viewId == mLastClickViewId) {\n            return true;\n        } else {\n            mLastClickTime = time;\n            mLastClickViewId = viewId;\n            return false;\n        }\n    }\n}<\/code><\/pre>\n<h3>\u7f16\u5199Aspect AOP\u5904\u7406\u7c7b<\/h3>\n<pre><code class=\"language-java\">@Aspect\npublic class SingleClickAspect {\n    private static final long DEFAULT_TIME_INTERVAL = 5000;\n\n    \/** \n     * \u5b9a\u4e49\u5207\u70b9\uff0c\u6807\u8bb0\u5207\u70b9\u4e3a\u6240\u6709\u88ab@SingleClick\u6ce8\u89e3\u7684\u65b9\u6cd5\n     * \u6ce8\u610f\uff1a\u8fd9\u91cccn.apblog.common.annotation.SingleClick\u9700\u8981\u66ff\u6362\u6210\u5177\u4f53\u9879\u76ee\u4e2dSingleClick\u8fd9\u4e2a\u7c7b\u7684\u5168\u8def\u5f84\n     *\/\n    @Pointcut(&quot;execution(@cn.apblog.common.annotation.SingleClick * *(..))&quot;)\n    public void methodAnnotated() {}\n\n    \/** \n     * \u5b9a\u4e49\u4e00\u4e2a\u5207\u9762\u65b9\u6cd5\uff0c\u5305\u88f9\u5207\u70b9\u65b9\u6cd5\n     *\/\n    @Around(&quot;methodAnnotated()&quot;)\n    public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {\n        \/\/ \u53d6\u51fa\u65b9\u6cd5\u7684\u53c2\u6570\n        View view = null;\n        for (Object arg : joinPoint.getArgs()) {\n            if (arg instanceof View) {\n                view = (View) arg;\n                break;\n            }\n        }\n        if (view == null) {\n            return;\n        }\n        \/\/ \u53d6\u51fa\u65b9\u6cd5\u7684\u6ce8\u89e3\n        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();\n        Method method = methodSignature.getMethod();\n        if (!method.isAnnotationPresent(SingleClick.class)) {\n            return;\n        }\n        SingleClick singleClick = method.getAnnotation(SingleClick.class);\n        \/\/ \u5224\u65ad\u662f\u5426\u5feb\u901f\u70b9\u51fb\n        if (!ClickUtil.isFastDoubleClick(view, singleClick.value())) {\n            \/\/ \u4e0d\u662f\u5feb\u901f\u70b9\u51fb\uff0c\u6267\u884c\u539f\u65b9\u6cd5\n            joinPoint.proceed();\n        }\n    }\n}<\/code><\/pre>\n<h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java\">private void initView() {\n    btTest = findViewById(R.id.bt_test);\n    btTest.setOnClickListener(new View.OnClickListener() {\n        \/\/ \u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u70b9\u51fb\u65f6\u95f4\u95f4\u9694\uff0c\u81ea\u884c\u4f20\u5165\u6beb\u79d2\u503c\u5373\u53ef\n        \/\/ @SingleClick(2000)\n        @SingleClick\n        @Override\n        public void onClick(View v) {\n            \/\/ do something\n        }\n    });\n}<\/code><\/pre>\n<p>\u53ea\u9700\u8981\u4e00\u4e2a\u6ce8\u89e3\uff0c\u5373\u5b8c\u6210\u4e86\u6309\u94ae\u7684\u9632\u6b62\u91cd\u590d\u70b9\u51fb\uff0c\u5176\u4ed6\u6240\u6709\u5de5\u4f5c\u4ea4\u7ed9\u7f16\u8bd1\u5668\uff0c\u4ee3\u7801\u6e05\u723d\u4e86\u5f88\u591a\u6709\u6728\u6709\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttps:\/\/www.jianshu.com\/p\/7b354eb8d0d3 \u4f20\u7edf\u5904\u7406\u65b9\u5f0f \uff081\uff09\u6bcf\u4e2a\u6309 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[100],"class_list":["post-1286","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-aop"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1286","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=1286"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1286\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}