android RadioButton相關(guān)

自定義單選按鈕樣式

 private void setData(List<Datas> v) {
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.leftMargin = DisplayUtil.dip2px(this, 5);
        layoutParams.bottomMargin = DisplayUtil.dip2px(this, 10);
        int index = 0;
        for (Datas data : v) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setGravity(Gravity.CENTER_VERTICAL);
            radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
            Drawable drawable=ContextCompat.getDrawable(this, R.drawable.radiobox_select);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                                drawable.getMinimumHeight());
            radioButton.setCompoundDrawables(drawable, null, null, null);
            radioButton.setCompoundDrawablePadding(DisplayUtil.dip2px(this, 10));
            radioButton.setPadding(1, 0, 0, 0);
            radioButton.setId(index++);
            radioButton.setTag(data.tag);
            radioButton.setText(data.reason);
            radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            radioButton.setTextColor(Color.BLACK);
            radioGroup.addView(radioButton, layoutParams);
        }
    }

可取消的單選按鈕設(shè)置

public class ToggleRadioButton extends AppCompatRadioButton {
    public ToggleRadioButton(Context context) {
        this(context, null);
    }

    public ToggleRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.radioButtonStyle);
    }

    public ToggleRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void toggle() {
        setChecked(!isChecked());
        if (!isChecked()) {
            ((RadioGroup) getParent()).clearCheck();
        }
    }
}

下面是RadioButton中源碼toggle()方法注釋

/**
     * {@inheritDoc}
     * <p>
     * If the radio button is already checked, this method will not toggle the radio button.
     */
    @Override
    public void toggle() {
        // we override to prevent toggle when the radio is already
        // checked (as opposed to check boxes widgets)
        if (!isChecked()) {
            super.toggle();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容