您的位置:首页 > 其它

自定义多行多列设置单选按钮

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答应出来的截图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐