0

我有一个片段,我尝试将动态编程单选按钮添加到单选组中。添加了单选按钮,但未设置文本。

这是我的代码:

private View myFragmentView;
private Context context;
private RadioGroup radioGrup;

private ArrayList<Language> allLanguages;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    allLanguages=MyAsyncTask.getAllLanguages();
    myFragmentView =  inflater
            .inflate(R.layout.language_view, container, false);
    context = this.getActivity().getApplicationContext();

    radioGrup = (RadioGroup)myFragmentView.findViewById(R.id.allLanguages);
    addRadioButton();

    return myFragmentView;
}

@SuppressLint({ "ResourceAsColor", "NewApi" })
private void addRadioButton(){
    for(Language language:allLanguages){
        RadioButton newRadioButton = new RadioButton(context);
        newRadioButton = new RadioButton(context);
        newRadioButton.setText("something");
        newRadioButton.setTextColor(android.R.color.black);
        newRadioButton.setLayoutParams 
          (new LayoutParams 
                  (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        radioGrup.addView(newRadioButton);
    }
}

有人可以帮助我吗?

4

1 回答 1

0

将您的代码修改为

private void addRadioButton(){
    for(Language language:allLanguages){
        RadioButton newRadioButton = new RadioButton(context);
        newRadioButton = new RadioButton(context);
        newRadioButton.setLayoutParams 
          (new LayoutParams 
                  (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        newRadioButton.setText("something");
        newRadioButton.setTextColor(android.R.color.black);
         radioGrup.addView(newRadioButton);
    }
}
于 2013-10-08T12:41:02.997 回答