如何隐藏下一个按钮,并在选择单选按钮组时显示

时间:2016-03-12 04:11:46

标签: java android

如何隐藏下一个按钮,仅在用户选择广播组按钮时显示

我有这个XML代码

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

和这个类代码

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();


                }
            }


        });


    }
}

如果有选定的单选按钮

,我该怎么做才能隐藏下一个按钮并显示

3 个答案:

答案 0 :(得分:0)

在按钮xml中,您应该使用:

  

机器人:能见度= “水涨船高”

在java中你应该使用:

  

btnDisplay.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

首先,我会从无线电组中取出按钮,让它低于它。然后在我的java代码中我会有这个函数:

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;

    }
}

确保每个单选按钮都设置了onClick =“onRadioButtonClicked”。

然后有一个名为make_button_visible()的函数,它包含以下行:

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

答案 2 :(得分:0)

在onCreate方法中添加以下代码

    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);
        }
    });