当用户单击第一个项目时,它应该被选中并且图像应该可见,当单击第二个项目时,第一个项目复选框应该变得不可见,依此类推。Inoroder 将图像放置在右侧,我通过放置在右侧制作了自定义可绘制图标。代码如下:
<?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" >
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:button="@android:color/transparent"
android:checked="true"
android:drawableRight="@drawable/tick"
android:text="Radio 0" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio 1"
android:layout_margin="10dp"
android:button="@null" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio 2"
android:layout_margin="10dp"
android:drawableRight="@drawable/tick"
android:button="@null"/>
</RadioGroup>
</RelativeLayout>
public class RadioButtonEx extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sortfilterclick);
radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radioButton1=(RadioButton)dialog.findViewById(R.id.radio1);
radioButton2=(RadioButton)dialog.findViewById(R.id.radio2);
radioButton3=(RadioButton)dialog.findViewById(R.id.radio3);
radioButton1.setOnClickListener(radioButtonOnClickListener);
radioButton2.setOnClickListener(radioButtonOnClickListener);
radioButton3.setOnClickListener(radioButtonOnClickListener);
private final OnClickListener radioButtonOnClickListener= new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* RadioButton rb = (RadioButton) v;
Toast.makeText(SortFilterPopupActivity.this, rb.getText(),
Toast.LENGTH_SHORT).show();*/
switch(v.getId()){
case R.id.radio2:
// this is not working
//Drawable d=radioButton2.setCompoundDrawables(null, null, R.drawable.tick, null);
Drawable tick=getResources().getDrawable(R.drawable.tick);
radioButton2.setCompoundDrawables(null, null, tick, null);
Toast.makeText(SortFilterPopupActivity.this, radioButton2.getText().toString(), Toast.LENGTH_SHORT)
.show();
break;
}
}
};}
预期输出为:
![enter image description here][1]