当用户点击第一个项目时,应该检查它并且图像应该可见,当点击第二个项目时,第一个项目勾选框应该变为不可见,依此类推。 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]
答案 0 :(得分:0)
由于你想单独隐藏单选按钮图标,也许你可以使用自定义样式并防止编程过多,我没有测试过,但它应该是这样的,
@绘制/ custom_btn_radio
<强> custom_radio.xml 强>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/a_transparent_drawable" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/a_transparent_drawable" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/radio_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/a_transparent_drawable" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/radio_off_selected" />
<item android:state_checked="false" android:drawable="@drawable/radio_off" />
<item android:state_checked="true" android:drawable="@drawable/a_transparent_drawable" />
</selector>
用自己的替换drawables,基本上 a_transparent_drawable 应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@android:color/transparent" />
</shape>
因此,当您按下单选按钮时,它应将其背景更改为透明,只留下文本。
希望它有所帮助。