【问题标题】:Hiding a RadioButton in Android在 Android 中隐藏 RadioButton
【发布时间】:2013-10-01 05:34:14
【问题描述】:

我想将 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 个问题的答案,但在某些情况下我想隐藏最后一个。

【问题讨论】:

    标签: android android-view


    【解决方案1】:

    您可以通过这种方式隐藏特定的单选按钮

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

    或者如果你使用 View Gone menas 单选按钮 hide with 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);
    

    【讨论】:

    • 有什么区别?在你的答案中?
    【解决方案2】:

    你好,像这样使用。

    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"/>
    

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

    【讨论】:

    • 这确实有效,但是在上面的格式中(我以编程方式将它隐藏在 onCreate 方法中)它没有。不幸的是,在这种情况下,让它在被点击时隐藏起来对我没有帮助。
    • @MarkWinterbottom 查看我编辑的答案,您可以在 XML 本身中完成。
    【解决方案3】:
        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");
                }
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多