【问题标题】:RadioGroup's doesn't show?RadioGroup的不显示?
【发布时间】:2011-12-06 23:52:20
【问题描述】:

我正在尝试将单选按钮动态添加到单选组,但这些按钮从不显示。不过,文字确实如此。我是初学者,所以请指出我做错的一切。

使用 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)

【问题讨论】:

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


    【解决方案1】:

    我认为您应该像这样执行您的 while 循环:

    while (currentAnswer < answers[currentQuestion]) {
    
            RadioButton rb = new RadioButton(this);
            //rb.setText("Answer no. " + currentAnswer);
            rg.addView(rb);
            currentAnswer++;
        }
    

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

    【讨论】:

    • 太棒了。我知道我检查了
    • 没问题 :) 这件事发生在我们所有人身上
    • 现在交换了 currentAnswers 和 answers[]。终于成功了:)
    【解决方案2】:

    替换此代码:

    int currentAnswer = 0;
    
    while (currentAnswer > answers[currentQuestion])
    

    到这里:

    int currentAnswer = 0;
    
    while (currentAnswer < answers[currentQuestion])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 2016-01-14
      • 1970-01-01
      相关资源
      最近更新 更多