为什么View无法显示Group Radio Widget?

时间:2012-02-08 01:12:46

标签: java android xml view

我在XML中添加了Group Radio小部件,但是当我调用selectLevel()时它不会在View中显示 我的代码:

public void selectLevel(){
     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);  
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  
        {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                //---displays the ID of the RadioButton that is checked---

                Toast toast = Toast.makeText(getApplicationContext(), String.format("Level %d", checkedId), Toast.LENGTH_SHORT);
                toast.show();


            }
        });

public void selectLevel(){ RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //---displays the ID of the RadioButton that is checked--- Toast toast = Toast.makeText(getApplicationContext(), String.format("Level %d", checkedId), Toast.LENGTH_SHORT); toast.show(); } });

1 个答案:

答案 0 :(得分:0)

我会将您的代码放在onCreate中,并将RadioGroup存储为您的活动字段。我认为这比调用另一个函数更有意义,而不能在它之外引用RadioGroup。