简体   繁体   中英

How to hide next button and show when radio group button is selected

How to I hide the next button and will only show when the user choose a radio group button

I have this XML code

<?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">



    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/assembly"
            android:checked="false"
            />

        <RadioButton
            android:id="@+id/csharp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/java"

            android:checked="false" />

        <Button
            android:id="@+id/btnDisplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NEXT"
            android:onClick="OnClick"
            />



    </RadioGroup>

</LinearLayout>

and this class code

package com.example.quiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Created by leste on 3/5/2016.
 */
public class CS_Category extends Activity {

    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private Button btnDisplay;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cs_category);
        addListenerOnButton();
    }

    public void addListenerOnButton() {

        radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
        btnDisplay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // get selected radio button from radioGroup
                int selectedId = radioSexGroup.getCheckedRadioButtonId();
                radioSexButton = (RadioButton) findViewById(selectedId);

                if (radioSexButton.getId() == R.id.assembly) {
                    Intent i = new Intent(CS_Category.this, CS_Assembly.class);
                    startActivity(i);
                } else {
                    if (radioSexButton.getId() == R.id.csharp) {
                        Intent i = new Intent(CS_Category.this, CS_Csharp.class);
                        startActivity(i);
                    } else {
                        if (radioSexButton.getId() == R.id.java) {
                            Intent i = new Intent(CS_Category.this, CS_Java.class);
                            startActivity(i);

                        }

                    }


                    Toast.makeText(CS_Category.this,
                            radioSexButton.getText(), Toast.LENGTH_SHORT).show();


                }
            }


        });


    }
}

What should I do to hide the next button and show only if there is a selected radio button

In your button xml you should use:

android:visibility="gone"

In java you should use:

btnDisplay.setVisibility(View.VISIBLE);

First of all I would take the button out of the radio group and just have it be below it. Then in my java code I would have this function:

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.assembly:
            if (checked)
                make_button_visible();
                break;
        case R.id.csharp:
            if (checked)
                make_button_visible();
                break;

    }
}

Make sure that each radio button has onClick="onRadioButtonClicked" set.

Then have a function called make_button_visible() that has these lines:

Button mButton=(Button)findViewById(R.id.btnDisplay);
mButton.setVisibility(View.VISIBLE);//This will make it visible

add below code in your onCreate method

    addListenerOnButton();
    btnDisplay.setVisibility(View.INVISIBLE);

    radioSexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (btnDisplay.getVisibility() == View.INVISIBLE)
                btnDisplay.setVisibility(View.VISIBLE);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM