如何安排盒子形式的RadioGroup按钮检查代码

时间:2015-10-30 01:24:24

标签: android xml

我希望我的按钮能够以盒子形式对齐,其中一个在另一个上面对齐,我尝试过垂直但它会破坏我的整个形状。我的Xml代码。我知道当每个单选按钮独立但我不能执行单选按钮的工作时我可以实现这一点。任何帮助将不胜感激。 这就是我所拥有的enter image description here     

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:paddingTop="25sp"
        android:paddingBottom="45sp"
        android:layout_above="@+id/start_button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="75dp">

        <RadioButton
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:id="@+id/subtract"
            android:text="−"

            android:shadowColor="#f99e0c"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:textColor = "#F000"
            android:textStyle="bold"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="65sp"
            android:background="@drawable/backcolor1"
            android:button="@null"
            android:layout_alignTop="@+id/radioGroup"
            android:layout_alignRight="@+id/settings"
            android:layout_alignEnd="@+id/settings" />

        <RadioButton
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:id="@+id/add"
            android:background="@drawable/backcolor1"
            android:text="+"
            android:shadowColor="#f99e0c"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:textColor = "#F000"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="65sp"
            android:gravity="center"
            android:button="@null"
            android:layout_alignTop="@+id/multiply"
            android:layout_alignLeft="@+id/divide"
            android:layout_alignStart="@+id/divide" />

        <RadioButton
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:id="@+id/divide"

            android:gravity="center"
            android:text="÷"

            android:shadowColor="#f99e0c"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:textColor = "#F000"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="65sp"
            android:background="@drawable/backcolor1"
            android:button="@null"
            android:layout_gravity="center_vertical"
            android:layout_below="@+id/radioGroup"
            android:layout_alignRight="@+id/settings"
            android:layout_alignEnd="@+id/settings" />

        <RadioButton
            android:layout_width="90dp"
            android:layout_height="90dp"

            android:gravity="center"

            android:background="@drawable/backcolor1"
            android:id="@+id/multiply"
            android:text="∗"
            android:shadowColor="#f99e0c"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:textColor = "#F000"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="65sp"
            android:button="@null"
            android:layout_below="@+id/radioGroup"
            android:layout_alignLeft="@+id/start_button"
            android:layout_alignStart="@+id/start_button" />

    </RadioGroup>

1 个答案:

答案 0 :(得分:0)

您可以尝试更新radiobox programmly,通常情况下,我在没有系统提供的radiobutton的情况下执行收音机功能,但是通过具有选择器背景的checkbox,当我选择其中一个项目时,我将更新所有的复选框到未选中状态,然后将单击的项目设置为选定状态。看起来效率低下,但据我所知,它总是很好用。

例如。 就像下面的代码在点击项目后一样。

for (int i = 0; i < payItemList.size(); i++) {
   PayItem tmpItem = payItemList.get(i);
   tmpItem.setSelected(false);
}
payItem.setSelected(!payItem.isChecked());

PayItem

public class PayItem extends LinearLayout {
    @InjectView(R.id.callsys_impay_pay_icon)
    protected ImageView mIconImgView;
    @InjectView(R.id.callsys_impay_subject)
    protected TextView mSubjectTv;
    @InjectView(R.id.callsys_impay_pay_body)
    protected TextView mBodyTv;
    @InjectView(R.id.minesys_pay_chkbox)
    protected CheckBox mCheckBox;
    private Context context;

    public PayItem(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public PayItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        View convertView = LayoutInflater.from(context).inflate(R.layout.kw_callsys_pay_item, this);
        ButterKnife.inject(this, convertView);
    }

    public void setEnable(boolean enable) {
        mSubjectTv.setTextColor(context.getResources().getColor(R.color.font_02));
        if (!enable) {
            mSubjectTv.setTextColor(context.getResources().getColor(R.color.font_gray));
        }
    }

    public boolean isChecked() {
        return mCheckBox.isChecked();
    }

    public void setSelected(boolean selected) {
        mCheckBox.setChecked(selected);
    }

    public void setContent(String icon, String subject, String body, boolean choosen) {
        int width = JApplication.getJContext().dip2px(48);
        KaowoImageLoader.loadImg(mIconImgView, icon, width, width, R.drawable.default_image);
        mSubjectTv.setText(subject);
        mBodyTv.setText(body);
        mCheckBox.setChecked(choosen);
    }

    public void setEnableSelectedClick(boolean enable) {
        setEnable(enable);
        setSelected(enable);
        setClickable(enable);
    }
}