带有自定义视图的RadioButton Android
我正在尝试实现一个带有自定义视图的单选按钮,如下面的屏幕截图 单选按钮的计数将是动态的。我试图创建一个带有自定义视图的RadioButton Android,android,android-layout,radio-button,radio-group,Android,Android Layout,Radio Button,Radio Group,我正在尝试实现一个带有自定义视图的单选按钮,如下面的屏幕截图 单选按钮的计数将是动态的。我试图创建一个Radiogroup,并以编程方式使用addView将RadioButton添加到Radiogroup。但是不能像附加屏幕截图中那样设置文本。因此,我计划添加一个LinearLayout,当单击LinearLayout时,我将启用附加到LinearLayout的单选按钮,并禁用其余按钮。这是正确的方法还是有其他方法来实现这一点 我也搜索了很多。但我找不到类似的东西。因此,请任何人向我推荐正确的
Radiogroup
,并以编程方式使用addView
将RadioButton
添加到Radiogroup
。但是不能像附加屏幕截图中那样设置文本。因此,我计划添加一个LinearLayout
,当单击LinearLayout
时,我将启用附加到LinearLayout
的单选按钮,并禁用其余按钮。这是正确的方法还是有其他方法来实现这一点
我也搜索了很多。但我找不到类似的东西。因此,请任何人向我推荐正确的方法。您可以使用自定义Listview进行操作
活动列表视图.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
自定义列表项:单选按钮\u item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioButtonLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:layout_margin="10dp">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.9"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/textCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code XYZ" />
<TextView
android:id="@+id/textCond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Rs.100 off if you purchase more than.." />
</LinearLayout>
<TextView
android:id="@+id/textValid"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Valid until dd/mm/yyyy" />
</LinearLayout>
现在使用自定义适配器从“活动”添加内容:
public class RadioButtonActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
// RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
final List<ItemContents> saveItems = new ArrayList<>();
String code = null, condition = null, valid = null;
// save your values here to the ItemContent List. usign dummy values as of now
for (int i = 1; i < 6; i++) {
code = "CodeXYZ000" + i;
condition = "Purchase More than Rs." + i + "00";
valid = "Valid until Dec 30";
saveItems.add(itemContents(i, code, condition, valid));
}
final CustomAdapter adapter = new CustomAdapter(this, R.layout.activity_list_view, saveItems);
ListView listView = (ListView) findViewById(R.id.radioGroup);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelectedIndex(position); // set selected position and notify the adapter
adapter.notifyDataSetChanged();
}
});
}
private static ItemContents itemContents(int i, final String code, final String condition, final String valid) {
ItemContents itemContent = new ItemContents();
itemContent.setId(i);
itemContent.setmCode(code);
itemContent.setmCondition(condition);
itemContent.setmValid(valid);
return itemContent;
}
}
公共类RadioButtonActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u视图);
//放射组放射组=(放射组)findViewById(R.id.RadioGroup);
最终列表saveItems=new ArrayList();
字符串代码=null,条件=null,有效=null;
//将您的值保存到ItemContent列表中。从现在起使用签名虚拟值
对于(int i=1;i<6;i++){
code=“CodeXYZ000”+i;
条件=“购买超过卢比”+i+“00”;
valid=“有效期至12月30日”;
添加(itemContents(i、代码、条件、有效));
}
最终CustomAdapter=新CustomAdapter(此,R.layout.activity\u list\u视图,saveItems);
ListView ListView=(ListView)findViewById(R.id.radioGroup);
setAdapter(适配器);
listView.setChoiceMode(listView.CHOICE\u MODE\u SINGLE);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
adapter.setSelectedIndex(位置);//设置所选位置并通知适配器
adapter.notifyDataSetChanged();
}
});
}
私有静态ItemContents ItemContents(int i,最终字符串代码,最终字符串条件,最终字符串有效){
ItemContents itemContent=新的ItemContents();
itemContent.setId(i);
itemContent.setmCode(代码);
itemContent.setmCondition(条件);
itemContent.setmValid(有效);
返回项目内容;
}
}
和自定义适配器:
public class CustomAdapter extends ArrayAdapter<ItemContents> {
int selectedIndex = -1;
public CustomAdapter(Context context, int activity_radio_button, List<ItemContents> saveItems) {
super(context, activity_radio_button, saveItems);
}
public void setSelectedIndex(int index) {
selectedIndex = index;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.radio_button_item, null);
}
RadioButton rbSelect = (RadioButton) v
.findViewById(R.id.radioButton);
if(selectedIndex == position){ // check the position to update correct radio button.
rbSelect.setChecked(true);
}
else{
rbSelect.setChecked(false);
}
ItemContents itemContents = getItem(position);
if (itemContents != null) {
TextView textCode = (TextView) v.findViewById(R.id.textCode);
TextView textCond = (TextView) v.findViewById(R.id.textCond);
TextView textValid = (TextView) v.findViewById(R.id.textValid);
textCode.setText(itemContents.getmCode());
textCond.setText(itemContents.getmCondition());
textValid.setText(itemContents.getmValid());
}
return v;
}
}
公共类CustomAdapter扩展了ArrayAdapter{
int selectedIndex=-1;
公共CustomAdapter(上下文上下文、int activity_单选按钮、列表保存项){
超级(上下文、活动单选按钮、保存项);
}
公共无效集合选定索引(整数索引){
selectedIndex=索引;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
拉平机vi;
vi=LayoutInflater.from(getContext());
v=vi.充气(R.布局.单选按钮项目,空);
}
RadioButton rbSelect=(RadioButton)v
.findViewById(R.id.radioButton);
如果(selectedIndex==position){//检查位置以更新正确的单选按钮。
rbSelect.setChecked(true);
}
否则{
rbSelect.setChecked(false);
}
ItemContents ItemContents=getItem(位置);
if(itemContents!=null){
TextView textCode=(TextView)v.findViewById(R.id.textCode);
TextView textCond=(TextView)v.findViewById(R.id.textCond);
TextView textValid=(TextView)v.findViewById(R.id.textValid);
setText(itemContents.getmCode());
textCond.setText(itemContents.getmCondition());
textValid.setText(itemContents.getmValid());
}
返回v;
}
}
还有你的输出(我在电视上做了这个,所以是在大屏幕上)
[您可以使用自定义列表视图。@FrankN.Stein感谢您的建议。下面是dhinakaran thennarasu发布的答案。我遵循并实现了这一点。