开发者

Radio buttons on click listenener

I have radio buttons in an application I am crea开发者_StackOverflowting. When the user clicks on one option, I want the other radio buttons to be unclickable.

I'm not sure how to do this, the radio buttons are set in an onclick listener and the logic is set within on click.


What about calling setEnabled(false) on the rest of the buttons? If there are too many buttons you can consider putting in an array:

RadioButton[] buttons; //you put here all buttons..

// then, when you catch the click, call a method like this
private void disableOtherButtons(RadioButton buttonClicked)
    for(RadioButton button : buttons){
        if( button != buttonClicked ){
            button.setEnabled(false);
        }
    }
}


If your using XML you can group them using RadioGroup, in the following example only one of the radio buttons is selectable at a time.

    <RadioGroup
        android:id="@+id/rg_configup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        >
        <RadioButton
            android:id="@+id/rb_configup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sales"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Regional"
            android:layout_above="@+id/rb_configup1"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Global"
            android:layout_above="@+id/rb_configup2"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ad-hoc"
            android:layout_above="@+id/rb_configup3"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
    </RadioGroup>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜