{"id":140,"date":"2023-02-18T19:56:53","date_gmt":"2023-02-18T11:56:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=140"},"modified":"2023-02-18T19:57:34","modified_gmt":"2023-02-18T11:57:34","slug":"android-4-4-radiobutton-setbuttondrawable-null-invalid","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/android-4-4-radiobutton-setbuttondrawable-null-invalid\/","title":{"rendered":"RadioButton\u5728Android 4.4\u53ca\u4ee5\u4e0b\u8c03\u7528setButtonDrawable(null)\u65e0\u6548\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u5728 Android 4.4 \u7cfb\u7edf\u4e0b\uff0cJava \u4ee3\u7801\u4e2d\u8c03\u7528 <code>RadioButton.setButtonDrawable(null)<\/code> \u65e0\u6548\uff0c\u800c\u5728 xml \u5e03\u5c40\u4e2d\u8bbe\u7f6e\u5143\u7d20\u5c5e\u6027 <code>android:button=&quot;@null&quot;<\/code> \u6709\u6548\uff0c\u5e76\u4e14\u5728 Android 5.0+ \u7cfb\u7edf\u4e0a\u8c03\u7528 Java \u4ee3\u7801\u4e5f\u6709\u6548\u3002<\/p>\n<p>\u521d\u6b65\u5206\u6790\uff0c\u5e94\u8be5\u662f\u7cfb\u7edf\u5dee\u5f02\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u76f4\u63a5\u6bd4\u8f83 Android 4.4 \u548c Android 5.0 \u7cfb\u7edf\u4ee3\u7801\u4e2d <code>RadioButton.setButtonDrawable()<\/code> \u65b9\u6cd5\u7684\u5dee\u5f02\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Android 4.4 \u7248\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public void setButtonDrawable(Drawable d) {\n    if (d != null) {\n        if (mButtonDrawable != null) {\n            mButtonDrawable.setCallback(null);\n            unscheduleDrawable(mButtonDrawable);\n        }\n        d.setCallback(this);\n        d.setVisible(getVisibility() == VISIBLE, false);\n        mButtonDrawable = d;\n        setMinHeight(mButtonDrawable.getIntrinsicHeight());\n    }\n    refreshDrawableState();\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u89c1\uff0c\u5728 Android 4.4 \u7cfb\u7edf\u4e2d\uff0c\u5728 <code>setButtonDrawable()<\/code> \u65b9\u6cd5\u4e2d\u4f20\u5165 null\uff0c\u662f\u6ca1\u6709\u4efb\u4f55\u4f5c\u7528\u7684\uff0c\u4ecd\u7136\u4f1a\u663e\u793a RadioButton \u9ed8\u8ba4\u7684\u6837\u5f0f\u3002<br \/>\n\u800c XML \u5e03\u5c40\u4e2d\u8bbe\u7f6e <code>android:button=&quot;@null&quot;<\/code>\uff0c\u4f1a\u5728 RadioButton \u8c03\u7528\u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u5c31\u5c06\u9884\u8bbe\u6837\u5f0f\u7684\u503c\u8bbe\u7f6e\u4e3a null\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 Java \u4ee3\u7801\u8bbe\u7f6e\u65e0\u6548\u800c XML \u5e03\u5c40\u8bbe\u7f6e\u6709\u6548\u7684\u539f\u56e0\u3002<\/p>\n<p>\u800c Android 5.0 \u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public void setButtonDrawable(Drawable d) {\n    if (mButtonDrawable != d) {\n        if (mButtonDrawable != null) {\n            mButtonDrawable.setCallback(null);\n            unscheduleDrawable(mButtonDrawable);\n        }\n        mButtonDrawable = d;\n        if (d != null) {\n            d.setCallback(this);\n            d.setLayoutDirection(getLayoutDirection());\n            if (d.isStateful()) {\n                d.setState(getDrawableState());\n            }\n            d.setVisible(getVisibility() == VISIBLE, false);\n            setMinHeight(d.getIntrinsicHeight());\n            applyButtonTint();\n        }\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u7cfb\u7edf\u4f1a\u5148\u6bd4\u8f83\u4f20\u5165\u7684\u503c\u548c\u9884\u8bbe\u6837\u5f0f\u7684\u503c\uff08mButtonDrawable\uff09\u662f\u5426\u76f8\u7b49\uff0c\u8fd9\u91cc\u4f20\u5165\u4e3a null\uff0c\u548c\u9884\u8bbe\u503c\u5f53\u7136\u4e0d\u76f8\u7b49\uff0c\u7136\u540e\u5c31\u5c06\u4f20\u5165\u7684\u503c\u8d4b\u503c\u7ed9\u9884\u8bbe\u6837\u5f0f\u3002\u5f53 RadioButton \u663e\u793a\u5230\u754c\u9762\u4e0a\u65f6\uff0c\u5c31\u6ca1\u6709\u9884\u8bbe\u6837\u5f0f\u3002<\/p>\n<p>\u77e5\u9053\u539f\u56e0\u540e\uff0c\u73b0\u5728\u6765\u770b\u770b\u5982\u4f55\u89e3\u51b3 Android 4.4 \u7cfb\u7edf\u4e0a\u8c03\u7528 <code>RadioButton.setButtonDrawable(null)<\/code> \u4ee3\u7801\u65e0\u6548\u7684\u95ee\u9898\u3002<\/p>\n<p>RadioButton \u9884\u8bbe\u6837\u5f0f\u7684\u503c mButtonDrawable \u662f\u5728\u62bd\u8c61\u7236\u7c7b CompoundButton \u4e2d\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u62ff\u5230\u8fd9\u4e2a\u5c5e\u6027\u5b57\u6bb5\uff0c\u7136\u540e\u5c06\u5176\u8bbe\u7f6e\u4e3a null \u5373\u53ef\uff0c\u4ee3\u7801\u5f88\u7b80\u5355\uff1a<\/p>\n<pre><code class=\"language-java\">if (Build.VERSION.SDK_INT &lt;= Build.VERSION_CODES.KITKAT) {\n    try {\n        Field field = radioButton.getClass().getSuperclass().getDeclaredField(&quot;mButtonDrawable&quot;);\n        field.setAccessible(true);\n        field.set(radioButton, null);\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Android 4.4 \u7cfb\u7edf\u4e0b\uff0cJava \u4ee3\u7801\u4e2d\u8c03\u7528 RadioButton.setButtonDrawa [&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":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/140","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=140"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}