我有RadioPutton和複選框,在ViewPager下的片段內以編程方式添加。以下是我添加RadioButton /複選框的方法。獲得增值的意見價值
public class TeaserExam extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//get all the checked values here
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1,questionList.get(position).getId(),
questionList.get(position).getQuestion(),questionList.get(position).getQuestionType(),questionList.get(position).getChoices());
}
}
public static class PlaceholderFragment extends Fragment {
public static PlaceholderFragment newInstance(int number, int questionID, String question, int questionType, List<Choices> choices) {
PlaceholderFragment fragment = new PlaceholderFragment();
ArrayList<Integer> choicesID = new ArrayList<>();
ArrayList<String> choicesLabel = new ArrayList<>();
for (int i = 0; i < choices.size(); i++){
choicesID.add(i,choices.get(i).getId());
choicesLabel.add(i,choices.get(i).getLabel());
}
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, number);
args.putInt("questionID", questionID);
args.putInt("questionType", questionType);
args.putInt("choicesSize", choices.size());
ParcelableChoices parcelableChoices = new ParcelableChoices(choicesID,choicesLabel);
args.putParcelable("parcelableChoices", parcelableChoices);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_teaser_exam, container, false);
int questionID = getArguments().getInt("questionID");
int questionType = getArguments().getInt("questionType");
int choicesSize = getArguments().getInt("choicesSize");
ParcelableChoices parcelableChoices = getArguments().getParcelable("parcelableChoices");
LinearLayout ll_choices = (LinearLayout)rootView.findViewById(R.id.ll_choices);
ArrayList<Integer> choicesIDs = parcelableChoices.getId();
ArrayList<String> choicesLabels = parcelableChoices.getLabel();
if (questionType == 2){
//checkboxes
for (int i = 0; i < choicesSize; i++){
CheckBox myChoices = new CheckBox(getContext());
myChoices.setId(choicesIDs.get(i));
myChoices.setText(choicesLabels.get(i));
myChoices.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ll_choices.addView(myChoices);
}
}else{
//radio group
RadioGroup rg = new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < choicesSize; i++){
RadioButton myChoices = new RadioButton(getContext());
myChoices.setId(choicesIDs.get(i));
myChoices.setText(choicesLabels.get(i));
myChoices.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rg.addView(myChoices);
}
ll_choices.addView(rg);
}
return rootView;
}
}
}
問題是現在我不知道如何檢索選中的單選按鈕/複選框的值。當我點擊一個只出現在最後一頁的按鈕時,我想獲得所有選中的值。
由於所有的視圖都添加到ll_choices,有沒有什麼辦法可以得到所有視圖的id和值添加在ll_choices下?
爲什麼不在創建它們的時候在領域中持有這些小部件? – CommonsWare
@CommonsWare對不起,我真的不明白你的意思 – imin
@imin請看下面的兩個答案。他們都詳細解釋了CommonsWare的含義。 –