RadioGroup中支持RadioButton多行多列

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_29586601/article/details/79088453

主要就是重写RadioGroup类:

package zq.com.radiogroupactivity;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by Administrator on 2018/1/17.
 */

public class MyRadioGroup extends RadioGroup {
    private OnCheckedChangeListener mOnCheckedChangeListener;

    public MyRadioGroup(Context context) {
        super(context);
    }

    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof LinearLayout) {
            int childCount = ((LinearLayout) child).getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = ((LinearLayout) child).getChildAt(i);
                if (view instanceof RadioButton) {
                    final RadioButton button = (RadioButton) view;
                    ((RadioButton) button).setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            ((RadioButton) button).setChecked(true);
                            checkRadioButton((RadioButton) button);
                            if (mOnCheckedChangeListener != null) {
                                mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId());
                            }
                            return true;
                        }
                    });
                }
            }
        }
        super.addView(child, index, params);

    }

    private void checkRadioButton(RadioButton radioButton) {
        View child;
        int radioCount = getChildCount();
        for (int i = 0; i < radioCount; i++) {
            child = getChildAt(i);
            if (child instanceof RadioButton) {
                if (child == radioButton) {
                    // do nothing
                } else {
                    ((RadioButton) child).setChecked(false);
                }
            } else if (child instanceof LinearLayout) {
                int childCount = ((LinearLayout) child).getChildCount();
                for (int j = 0; j < childCount; j++) {
                    View view = ((LinearLayout) child).getChildAt(j);
                    if (view instanceof RadioButton) {
                        final RadioButton button = (RadioButton) view;
                        if (button == radioButton) {
                            // do nothing
                        } else {
                            ((RadioButton) button).setChecked(false);
                        }
                    }
                }
            }
        }
    }
}
然后就是activity_main.xml

<?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="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Hello World!"/>

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

</LinearLayout>
主类:Mactivity.class

package zq.com.radiogroupactivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioFragment radioFragment = new RadioFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment,radioFragment);
        transaction.commit();

//        RadioGroup mRadio = findViewById(R.id.radiogroup);
//        mRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                Log.e(TAG,"选择:" + i);
//                Toast.makeText(MainActivity.this, "你点击的是按钮" + i, Toast.LENGTH_SHORT).show();
//
//            }
//        });
    }
}
package zq.com.radiogroupactivity;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Created by Administrator on 2018/1/17.
 */

public class RadioFragment extends Fragment {
    private String contextS = "";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.radiogroup, container, false);
        RadioGroup mRadio = view.findViewById(R.id.radiogroup);
        final RadioButton radio1 = view.findViewById(R.id.radio1);
        final RadioButton radio2 = view.findViewById(R.id.radio2);
        final RadioButton radio3 = view.findViewById(R.id.radio3);
        final RadioButton radio4 = view.findViewById(R.id.radio4);

        mRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                /**
                 * 获取每个radiobutton的值不能用原有的方法了,试了一下,原有的方法获取不到值了
                 *  因为这里radiobutton已经不是radiogroup的子view了,
                 *  通过radioGroup.getCheckedRadioButtonId()或者radioGroup.getCheckedRadioButtonId()
                 *  这2个方法都无法获取radiobutton的值了
                 *  所以想了个不成熟的方法
                 */
                switch (i){
                    case R.id.radio1:
                        contextS = radio1.getText().toString();
                        break;
                    case R.id.radio2:
                        contextS = radio2.getText().toString();
                        break;
                    case R.id.radio3:
                        contextS = radio3.getText().toString();
                        break;
                    case R.id.radio4:
                        contextS = radio4.getText().toString();
                        break;
                }

                Toast.makeText(getActivity(), "你点击的是: "+ contextS, Toast.LENGTH_SHORT).show();

            }
        });
        return view;
    }
}
<?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="match_parent">

    <zq.com.radiogroupactivity.MyRadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/button_pressed"
                android:button="@color/transparent"
                android:gravity="center"
                android:text="按钮1"
                android:textColor="@drawable/button_textcolor"/>

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/button_pressed"
                android:button="@color/transparent"
                android:gravity="center"
                android:text="按钮2"
                android:textColor="@drawable/button_textcolor"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/button_pressed"
                android:button="@color/transparent"
                android:gravity="center"
                android:text="按钮3"
                android:textColor="@drawable/button_textcolor"/>

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/button_pressed"
                android:button="@color/transparent"
                android:gravity="center"
                android:text="按钮4"
                android:textColor="@drawable/button_textcolor"/>
        </LinearLayout>

    </zq.com.radiogroupactivity.MyRadioGroup>

</LinearLayout>
上面是radiogroup.xml

button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_shape_pressed" android:state_checked="true"/>
    <item android:drawable="@drawable/button_shape" android:state_checked="false"/>
    <!--<item android:drawable="@drawable/button_shape_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_shape" android:state_pressed="false"/>
    <item android:drawable="@drawable/button_shape_pressed" android:state_selected="true"/>
    <item android:drawable="@drawable/button_shape" android:state_selected="false"/>
    <item android:drawable="@drawable/button_shape_pressed" android:state_enabled="true"/>
    <item android:drawable="@drawable/button_shape" android:state_enabled="false"/>-->

</selector>

button_shape_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval"
       android:useLevel="false">
    <solid android:color="@color/red"/>
    <!--<stroke
        android:width="1dp"
        android:color="@color/white"/>-->
    <size android:width="50dp"
          android:height="50dp"/>
</shape>
button_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval"
       android:useLevel="false">
    <solid android:color="@color/yellow1"/>
    <!--<stroke
        android:width="1dp"
        android:color="@color/white"/>-->
    <size android:width="50dp"
          android:height="50dp"/>
</shape>
button_textcolor.xml

<?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/black" android:state_checked="false"/>
</selector>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="transparent">#000000</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="red">#FF0000</color>
    <color name="yellow">#FFFF00</color>
    <color name="yellow1">#FF9a31</color>
    <color name="blue">#76EE00</color>
    <color name="blue2">#00ff00</color>
    <color name="purple">#B23AEE</color>
    <color name="green">#4876FF</color>
    <color name="pink">#FFBBFF</color>
    <color name="red_pink">#FF66FF</color>
    <color name="blue1">#0050f0</color>
</resources>
demo下载地址:http://download.csdn.net/download/qq_29586601/10212444














展开阅读全文

RadioGroup RadioButton 无法显示出来

06-27

我按着书上的步骤操作,创建一个Button后点击一下,跳转到RadioGroup页面,现在出现这样一个问题:rn代码A是我自己写的,因为在写TextView等控件的时候都是这样的,所以我在RadioGroup也这么写,但是只能跳转,RadioButotn并没有显示出来rnrn代码B是例子里面的,可以正确的显示rnrn我想问下为什么代码A不正确呢?(注:其他的都一样,就代码A与B的区别)rnrn还有代码B中的 implements View.OnClickListener 以及 setOnClickListener(this); 应该怎么理解??rn谢谢rnrn//代码 Arnpublic class RadioGroupActivity extends Activityrnrn RadioGroup mRadioGroup;rn rn public void onCrearte(Bundle save)rn rn super.onCreate(save); rn setTitle("RadioGroupActivity");rn setContentView(R.layout.radiogroup);rn find_and_modify_radio_group();rn rn rn private void find_and_modify_radio_group()rn rn mRadioGroup = (RadioGroup)findViewById(R.id.menu);rn Button clearButton = (Button)findViewById(R.id.clear);rn clearButton.setOnClickListener(clearButtonListener);rn rn rn private Button.OnClickListener clearButtonListener = new Button.OnClickListener()rn rn public void onClick(View v)rn rn mRadioGroup.clearCheck();rn rn ;rnrnrnrn//代码 Brn/*rnpublic class RadioGroupActivity extends Activity implements View.OnClickListenerrnrn private RadioGroup mRadioGroup;rn protected void onCreate(Bundle save)rn rn super.onCreate(save);rn setContentView(R.layout.radiogroup);rn setTitle("RadioGroupActivity");rn mRadioGroup = (RadioGroup)findViewById(R.id.menu);rn Button clearButton = (Button)findViewById(R.id.clear);rn clearButton.setOnClickListener(this);rn rn public void onClick(View v)rn rn mRadioGroup.clearCheck();rn rnrn*/ 论坛

没有更多推荐了,返回首页