单选按钮onClick操作

时间:2018-07-25 18:21:23

标签: android onclick radio-button

单击单选按钮后,我需要打开最后的线性布局 给定的是我的xml代码 我需要显示最后的线性布局,在单击男性单选按钮后,它应该具有2个按钮,直到它应该被隐藏为止。 我尝试了很多,但我失败了,我仍然学习雄激素                                                 ...... ................... ..................... ................ 如果有任何建议,请amil @ byhaqi.fuzail@yahoo.com

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">





    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Button"
        tools:layout_editor_absoluteX="76dp"
        tools:layout_editor_absoluteY="167dp" />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/rg">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male"
            android:paddingLeft="10dp"

            android:backgroundTint="@color/colorPrimaryDark"
            android:id="@+id/rone"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:id="@+id/rtwo"
            android:layout_height="wrap_content"
            android:text="female"
            android:paddingLeft="10dp"
            android:backgroundTint="@color/colorPrimaryDark"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/linear"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/colorPrimaryDark"
        android:visibility="gone"
        >
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Button"
        tools:layout_editor_absoluteX="76dp"
        tools:layout_editor_absoluteY="167dp" />

    </LinearLayout>

    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

RadioGroup radioGrp = (RadioGroup) findViewById(R.id.yourRadioGroup);        
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedRadioId) {
        // checkedRadioId is the RadioButton selected
        // You can set Visibility Logic here if checkId is of Male Radio button
    }
});