2013-10-01 129 views
2

我想將RadioButton的可見性設置爲INVISIBLE或GONE。由於某種原因,這是行不通的。在Android中隱藏RadioButton

RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id); 
myRadioButton.setVisibility(View.GONE); 

myRadioButton.setVisibility(View.INVISIBLE); 

沒有錯誤返回,它只是沒有做任何事情。

但是我已經試過這在RadioGroup中

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers); 
myRadioGroup.setVisibility(View.INVISIBLE); 

,它工作正常隱藏整個集團。有沒有辦法隱藏其中一個RadioButton?我有一組3個問題的答案,但在某些情況下,我想隱藏最後一個問題。

回答

6

可以隱藏特定的單選按鈕,這樣

RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio); 
myRadioButton.setVisibility(View.INVISIBLE); 

,或者如果您使用View飄menas單選按鈕隱藏與sapce

RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio); 
myRadioButton.setVisibility(View.GONE); 

在這種情況下,不隱藏無線電集團

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers); 
myRadioGroup.setVisibility(View.Visible); 
1

嗨,這樣使用。

RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id); 
myRadioButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      myRadioButton.setVisibility(View.INVISIBLE); 
     } 
    }); 

OR

<RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" 
     android:visibility="invisible"/> 

此代碼工作me.Hope這將幫助你。

+0

這是行不通的,howe ver以上面的格式(我用編程方式將它隱藏在onCreate方法中),但它沒有。不幸的是,在點擊時隱藏它並不能幫助我解決這種情況。 –

+0

@MarkWinterbottom看到我編輯的答案,你可以在XML本身。 – Nirmal

0
super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TableLayout bilgiAlani=(TableLayout)findViewById(R.id.bilgiAlani); 
    final RadioButton secim1 = (RadioButton) findViewById(R.id.butonSecim1); 
    final TextView bilgiMesaji=(TextView)findViewById(R.id.bilgiMesaji); 
    secim1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      if (secim1.isChecked()) { 
       Toast.makeText(getApplicationContext(), "secildi", 
         Toast.LENGTH_SHORT).show(); 
       bilgiAlani.setVisibility(View.VISIBLE); 
       bilgiMesaji.setText("birinci seicmbirinci seicmbirinci seicmbirinci seicmbirinci\n seicmbirinci seicm" + 
         "birinci seicmbirinci seicmbirinci seicmbirinci seicm" + 
         "birinci seicmbirinci seicm "); 
      } 
      else if(!secim1.isChecked()) 
      { 
       Toast.makeText(getApplicationContext(), "Secmekten Vazgecildi", 
         Toast.LENGTH_SHORT).show(); 
       bilgiAlani.setVisibility(View.GONE); 
       bilgiMesaji.setText("birinci secilmedi"); 
      } 

     } 
    });