自定义字体单选按钮未检测到单击

时间:2017-03-24 06:26:46

标签: java android xml android-layout

我用自定义字体制作了一个自定义单选按钮。这是单选按钮的代码:

IntPtr

以下是布局用法:

public class AvenirRadioButton extends RadioButton {

    /*
     * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
     */
    private static Typeface mTypeface;

    public AvenirRadioButton(final Context context) {
        this(context, null);
    }

    public AvenirRadioButton(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AvenirRadioButton(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/avenir_light.ttf");
        }
        setTypeface(mTypeface);
    }

}

radio_text_selector:

                     <RadioGroup
                        android:id="@+id/radioGroupType"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <com.app.utils.AvenirRadioButton
                            android:id="@+id/radioProject"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="@drawable/radio_selector"
                            android:button="@null"
                            android:checked="true"
                            android:gravity="center"
                            android:paddingBottom="@dimen/size_small"
                            android:paddingTop="@dimen/size_small"
                            android:text="@string/project"
                            android:textColor="@drawable/radio_text_selector"
                            android:textStyle="bold" />

                        <com.app.utils.AvenirRadioButton
                            android:id="@+id/radioSocial"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="@drawable/radio_selector"
                            android:button="@null"
                            android:gravity="center"
                            android:paddingBottom="@dimen/size_small"
                            android:paddingTop="@dimen/size_small"
                            android:text="@string/social"
                            android:textColor="@drawable/radio_text_selector"
                            android:textStyle="bold" />
                    </RadioGroup>

radio_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/white" android:state_checked="true" />
    <item android:color="@color/colorPrimary" />

</selector>

问题是我添加字体时无法检查单选按钮。我的意思是如果我使用RadioButton而不是AvenirRadioButton,检查发生。有什么我可以做的来保持字体和检查两者?

2 个答案:

答案 0 :(得分:1)

如果你甚至在自定义类中评论与字体相关的逻辑,它就不会像标准RadioButton一样工作,因为你错误地覆盖了构造函数,它从{{1}创建了一个Java对象}:

xml

答案 1 :(得分:0)

如果您只想更改字体,则可以使用以下代码,而不是创建自定义单选按钮:

RadioButton rb  = (RadioButton) findViewById(R.id.radioSocial);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/avenir_light.ttf");
rb.setTypeface(font);