我有一个主要的活动,我必须在每个屏幕上显示12个问题中的3个(保存在sql数据库中),并使用单选按钮Yes和No。所以我创建了一个片段类,并在我的主活动中调用了它三次。在我的next按钮上,我创建了我的主类的意图。因此,使用一个片段,我在4个屏幕上填充了12个问题。
现在,当我按下单选按钮时,我希望看到单选按钮针对哪个问题被单击,然后计算分数(每个问题都有单独的分数。这也在我的sqlite数据库中映射到每个问题上)我该怎么做?
发布于 2020-04-22 01:32:35
方法一:创建4个片段。
public class Fragment1 extends Fragment {
private RadioButton radioButton;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment2 extends Fragment {
private RadioButton radioButton;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment3 extends Fragment {
private RadioButton radioButton;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
public class Fragment4 extends Fragment {
private RadioButton radioButton;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// radioButton.setOnCheckedChangeListener...
}
}
方法2:通过传递4个不同的整数包创建1个片段。
Bundle bundle1 = new Bundle();
bundle1.putInt("questions1" , 1);
Bundle bundle2 = new Bundle();
bundle2.putInt("questions2" , 2);
Bundle bundle3 = new Bundle();
bundle3.putInt("questions3" , 3);
Bundle bundle4 = new Bundle();
bundle4.putInt("questions4" , 4);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle1);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle2);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle3);
Fragment fragment = new QuestionsFragment();
fragment.setArguments(bundle4);
QuestionsFragment
中的onCreateView
使用Bundle bundle1 = getArguments();
获取整数标志。
https://stackoverflow.com/questions/61349091
复制相似问题