2016-03-12 29 views
2

如何我隱藏下一個按鈕,當用戶選擇一個單選按鈕組按鈕如何隱藏下一個按鈕,並顯示在單選按鈕組按鈕被選中

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


       } 
      } 


     }); 


    } 
} 

我應該怎麼做隱藏下一個按鈕,只能說明,如果有一個選定的單選按鈕

+0

隱藏按鈕,任何變化發生在無線電組,顯示按鈕。 –

+0

當您選擇特定的單選按鈕時,您是否想要隱藏按鈕?就像選擇了java一樣,你想隱藏按鈕。 – Harry

+0

在我的下一個按鈕的XML中,它被聲明爲android:visibility =「gone」 所以當我點擊任何單選按鈕時,它會顯示我的下一個按鈕。 –

回答

0

在按鈕XML,你應該使用:

機器人:知名度= 「水漲船高」

在java中,你應該使用:

btnDisplay.setVisibility(View.VISIBLE);

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 
+0

make_button_visible(); –

+0

make_button_visible(); 無法解析方法 和我應該在哪裏放 utton mButton =(按鈕)findViewById(R.id.btnDisplay); mButton.setVisibility(View.VISIBLE); –

+0

做了一個叫做「make_button_visible()」的函數,並把這些行放在裏面。 –

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); 
     } 
    }); 
相關問題