0

当用户单击第一个项目时,它应该被选中并且图像应该可见,当单击第二个项目时,第一个项目复选框应该变得不可见,依此类推。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]
4

1 回答 1

0

由于您想单独隐藏单选按钮图标,也许您可​​以使用自定义样式并防止过度编程,我没有对此进行测试,但应该是这样的,

@drawable/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>

因此,当您按下单选按钮时,它应该将其背景更改为透明,只留下文本。

希望能帮助到你。

于 2014-12-12T12:40:52.553 回答