RadioGroup的没有显示?

时间:2011-12-06 15:34:50

标签: android android-layout radio-button radio-group

我正在尝试动态地将radiobuttons添加到radiogroups,但按钮永远不会出现。但是,文本确实如此。我是初学者,所以请指出我做错的一切。

使用APKv7(2.1我认为)。

package test.test2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;

public class TipsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);


    //create objects
    ScrollView scrollv = new ScrollView(this);
    LinearLayout linlay = new LinearLayout(this);
    linlay.setOrientation(LinearLayout.VERTICAL);
    scrollv.addView(linlay);

    int currentQuestion = 0, questions = 3;
    int [] answers = new int[4];
    answers[0] = 3;
    answers[1] = 3;
    answers[2] = 4;
    answers[3] = 6;
    while (questions > currentQuestion) {           

        RadioGroup rg = new RadioGroup(this);

        TextView tv = new TextView(this);
        tv.setText("Question no. " + currentQuestion);



        int currentAnswer = 0;

        while (currentAnswer > answers[currentQuestion]) {

            RadioButton rb = new RadioButton(this);
            //rb.setText("Answer no. " + currentAnswer);
            rg.addView(rb);
            currentAnswer++;
        }

        linlay.addView(rg);
        linlay.addView(tv);
        currentQuestion++;  
    }
    setContentView(scrollv);
}
}

(notEnoughContext notEnoughContext)

2 个答案:

答案 0 :(得分:1)

替换此代码:

int currentAnswer = 0;

while (currentAnswer > answers[currentQuestion])

对此:

int currentAnswer = 0;

while (currentAnswer < answers[currentQuestion])

答案 1 :(得分:1)

我认为你应该这样做你的while循环:

while (currentAnswer < answers[currentQuestion]) {

        RadioButton rb = new RadioButton(this);
        //rb.setText("Answer no. " + currentAnswer);
        rg.addView(rb);
        currentAnswer++;
    }

问题是currentAnswer小于任何答案......