在Android中隐藏单选按钮

时间:2013-10-01 05:34:14

标签: android android-view

我想将RadioButton的可见性设置为INVISIBLE或GONE。由于某些原因,这不起作用。

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

它可以隐藏整个组。有没有办法隐藏其中一个RadioButtons?我有一组3个问题的答案,但在某些情况下我想隐藏最后一个。

3 个答案:

答案 0 :(得分:6)

你可以用这种方式隐藏特定的单选按钮

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

或者如果您使用View Gone 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 :(得分: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);
        }
    });

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

此代码对我有用。希望这对您有帮助。

答案 2 :(得分: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");
            }

        }
    });