自定义多行多列设置单选按钮
2016-11-03 15:00
274 查看
效果图values里面设置的
<?xml version="1.0" encoding="utf-8"?> <resources>
<!--多行多列 单项选择-->
<declare-styleable name="attrs_choice"> <attr name="attr_rb1" format="string"/> <attr name="attr_rb2" format="string"/> <attr name="attr_rb3" format="string"/> <attr name="attr_rb4" format="string"/> </declare-styleable>
</resources>这是自定义控件
import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import com.sj.yinjiaoyun.xuexi.R; /** * Created by ${沈军 961784535@qq.com} on 2016/11/3. * 自定义 多行多列的 单项选则 */ public class SingleChoiceView extends LinearLayout{ RadioButton rb1; RadioButton rb2; RadioButton rb3; RadioButton rb4; RadioGroup gp1; RadioGroup gp2; Context context; public SingleChoiceView(Context context) { super(context); init(context); } public SingleChoiceView(Context context, AttributeSet attrs) { super(context, attrs); init(context); getValues(attrs); } public SingleChoiceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); getValues(attrs); } //初始化控件 private void init(Context context){ this.context=context; View view= LayoutInflater.from(context).inflate(R.layout.view_major_direction,this); rb1= (RadioButton) view.findViewById(R.id.major_direction_rb1); rb2= (RadioButton) view.findViewById(R.id.major_direction_rb2); rb3= (RadioButton) view.findViewById(R.id.major_direction_rb3); rb4= (RadioButton) view.findViewById(R.id.major_direction_rb4); gp1= (RadioGroup) view.findViewById(R.id.major_direction_group1); gp2= (RadioGroup) view.findViewById(R.id.major_direction_group2); //为每个RadioButton设置监听器 rb1.setOnClickListener(btnListener1); rb2.setOnClickListener(btnListener2); rb3.setOnClickListener(btnListener3); rb4.setOnClickListener(btnListener4); } BtnSelected btnListener1 = new BtnSelected("1"); BtnSelected btnListener2 = new BtnSelected("2"); BtnSelected btnListener3 = new BtnSelected("3"); BtnSelected btnListener4 = new BtnSelected("4"); private void getValues(AttributeSet attrs){ TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.attrs_choice); String str1=array.getString(R.styleable.attrs_choice_attr_rb1); rb1.setText(str1); String str2=array.getString(R.styleable.attrs_choice_attr_rb1); rb1.setText(str2); String str3=array.getString(R.styleable.attrs_choice_attr_rb1); rb1.setText(str3); String str4=array.getString(R.styleable.attrs_choice_attr_rb1); rb1.setText(str4); array.recycle(); } /** * * @return 返回选中按钮的控件 */ public RadioButton getChoiceValues(){ RadioButton rb=null; switch(strBtnSelected){ case "1": rb=rb1; break; case "2": rb=rb2; break; case "3": rb=rb3; break; case "4": rb=rb4; break; } return rb; } /** * * @param stnSelected 数字对应的按钮控件 * @param values 给这个控件设置的值 */ public void setChoiceValues(int stnSelected,String values){ switch(stnSelected){ case 1: rb1.setText(values); break; case 2: rb2.setText(values); break; case 3: rb3.setText(values); break; case 4: rb4.setText(values); break; } } //用于保存当前被选中的按钮 String strBtnSelected = "unInit"; //监听类,每个Radiobutton均对Click动作进行监听:若用户点击的是Group1的按钮(A或B或C),则清除Group2中按钮被选中的状态 //若用户点击的是Gourp2的按钮(B或C或D),则清除Group1中按钮被选中的状态。其中的1~6分别对应按钮A~F class BtnSelected implements OnClickListener { public BtnSelected(String str) { bntID = str; } final public String bntID; @Override public void onClick(View arg0) { // TODO Auto-generated method stub strBtnSelected = bntID; if (bntID.equals("1") || bntID.equals("2")) { gp2.clearCheck(); } else if (bntID.equals("3") || bntID.equals("4") ) { gp1.clearCheck(); } } }; }布局中使用
<?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"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:hint="首页"/><com.sj.yinjiaoyun.xuexi.view.SingleChoiceViewandroid:id="@+id/SingleChoice"app:attr_rb1="java"app:attr_rb3="ios布局设置的值"app:attr_rb4="android"android:layout_width="match_parent"android:layout_height="wrap_content"/><!--确认选择--><Buttonandroid:onClick="onclick"android:id="@+id/major_direction_sure"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:paddingTop="10dp"android:paddingBottom="10dp"android:paddingLeft="30dp"android:paddingRight="30dp"android:hint="确认选择"/></LinearLayout>Activity或者Fragment代码中使用
public class HomeFragment extends Fragment {String TAG="homefragment";SingleChoiceView single;Button btn;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view =inflater.inflate(R.layout.fragment_home,container,false);initView(view);return view;}private void initView(View view) {single= (SingleChoiceView) view.findViewById(R.id.SingleChoice);btn= (Button) view.findViewById(R.id.major_direction_sure);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.i(TAG, "onclick: ");RadioButton rb=single.getChoiceValues();Log.i(TAG, "onclick: "+rb.getText().toString());}});single.setChoiceValues(2,"web这是代码中设置的值");single.setChoiceValues(3,"ios这是代码中设置的值");}}效果图最后log答应出来的截图
相关文章推荐
- 自定义reportviewer打印设置按钮的另类实现方法
- 关于VC单选按钮不能设置变量的问题以及Group属性的设置
- iOS-自定义单选复选按钮
- iCheck:超级精美的自定义复选框 & 单选按钮
- android 单选按钮 RadioButton 自定义图片左边距
- 如何设置自定义按钮的样式
- :1)复选框中文字在左边;2)最下部为水平滚动条。水平滚动条最小值为4、最大值为72;且在窗体Load事件中通过代码设置;3)单击任何复选框,标签上文字样式都发生变化;4)单击任意单选按钮,标签上文字
- 各浏览器对表单元素单选按钮组设置非 CDATA 标准的 name 属性值解析不同
- HF9009: 各浏览器对表单元素单选按钮组设置非 CDATA 标准的 name 属性值解析不同
- WIN32汇编-单选按钮组的设置
- VC单选按钮设置Group属性后有些按钮对应的变量值总是0的问题
- 如何读取单选按钮被选中的值,如何设置单选按钮
- GridView中自定义单选按钮列的实现!(修改)
- Android之自定义背景Button按钮、自定义形状Button的全攻略、设置字体颜色
- 美化表单——自定义单选按钮和复选按钮
- 关于VC单选按钮不能设置变量的问题以及Group属性的设置
- GridView中自定义单选按钮列的实现!
- 关于VC单选按钮不能设置变量以及Group属性的设置问题
- android 自定义RadioButton(单选按钮)图标随便定.
- 1)复选框中文字在左边;2)水平滚动条最小值为4、最大值为72;且在窗体Load事件中通过代码设置;3)单击任何复选框,标签上文字样式都发生变化;4)单击任意单选按钮,标签上文字字体都发生改变;5)拖