簡體   English   中英

如何隱藏下一個按鈕並在選擇單選按鈕時顯示

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

如何隱藏下一個按鈕,並且僅當用戶選擇單選按鈕時才會顯示

我有這個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();


                }
            }


        });


    }
}

我應該怎么做才能隱藏下一個按鈕並僅在選中單選按鈕時顯示

在您的按鈕xml中,您應該使用:

機器人:能見度=“水漲船高”

在Java中,您應該使用:

btnDisplay.setVisibility(View.VISIBLE);

首先,我將按鈕從廣播組中移出並放在其下方。 然后在我的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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM