设置单选按钮文本

时间:2013-10-08 12:31:14

标签: android

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

这是我的代码:

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);
    }
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分: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);
    }
}