单选按钮的自定义布局

问题描述 投票:0回答:2

我做了一个我想为RadioButton实现的自定义布局。android类的代码在这里:

public class MyRadioButton extends LinearLayout implements View.OnClickListener{
    private ImageView iv;
    private TextView tv;
    private RadioButton rb;

    private View view;

    public MyRadioButton(Context context) {
        super(context);
        view = View.inflate(context, R.layout.my_radio_button, this);
        setOrientation(HORIZONTAL);

        rb = (RadioButton) view.findViewById(R.id.radioButton1);
        tv = (TextView) view.findViewById(R.id.textView1);
        iv = (ImageView) view.findViewById(R.id.imageView1);

        view.setOnClickListener(this);
        rb.setOnCheckedChangeListener(null);
    }

    public void setImageBitmap(Bitmap bitmap) {
        iv.setImageBitmap(bitmap);
    }

    public View getView() {
        return view;
    }

    @Override
    public void onClick(View v) {

        boolean nextState = !rb.isChecked();

        LinearLayout lGroup = (LinearLayout)view.getParent();
        if(lGroup != null){
            int child = lGroup.getChildCount();
            for(int i=0; i<child; i++){
                //uncheck all
                ((RadioButton)lGroup.getChildAt(i).findViewById(R.id.radioButton1)).setChecked(false);
            }
        }

        rb.setChecked(nextState);
    }

    public void setImage(Bitmap b){
        iv.setImageBitmap(b);
    }

    public void setText(String text){
        tv.setText(text);
    }

    public void setChecked(boolean isChecked){
        rb.setChecked(isChecked);
    }
}

和布局代码在这里

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="top"
        android:text="" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wow_visa_prepaid" />

    </LinearLayout>
</LinearLayout>

目前,我不知道如何将继承从LinearLayout更改为RadioButton并保持相同的布局。

android radio-button android-custom-view
2个回答
0
投票

我已经尝试了以下代码,并且可以正常工作且没有错误:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<com.example.myapplication.MyRadioButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

更改以下代码后:

public MyRadioButton(Context context) {

至以下代码:

 public MyRadioButton(Context context, AttributeSet attributes) {

这是您需要的解决方案吗?


0
投票

有许多实现此类单选按钮的库,例如

https://github.com/ceryle/RadioRealButton
© www.soinside.com 2019 - 2024. All rights reserved.