java - 如何在Android中获取所选单选按钮的ID
问题描述
在我的项目中,我为用户创建了 3 个用于多项选择的单选按钮。我在 SQLite 数据库中有一些问题和答案,我正在检索这些问题和答案,Activity
然后将它们设置为RadioGroup
. 它工作正常,但现在当我想获取所选单选按钮的所有 ID 然后计算它时。我正在使用'com.stepstone.stepper:material-stepper:4.3.1'
库(参考链接)。
我的活动
public class KlasifikasiActivity extends AppCompatActivity implements StepperLayout.StepperListener {
@Override
public void onCompleted(View completeButton) {
Toast.makeText(this, "onCompleted!", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(VerificationError verificationError) {
Toast.makeText(this, "onError! -> " + verificationError.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onStepSelected(int newStepPosition) {
Toast.makeText(this, "onStepSelected! -> " + newStepPosition, Toast.LENGTH_SHORT).show();
}
@Override
public void onReturn() {
finish();
}
}
我的步骤片段
public class StepFragmentExample extends Fragment implements Step {
public StepFragmentExample() {
// Required empty public constructor
}
RadioGroup mRadioGroup;
RadioButton mRadioButton1;
RadioButton mRadioButton2;
RadioButton mRadioButton3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Bundle bundle = this.getArguments();
Log.d("##", "onCreateView: " + bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) ;
View view = inflater.inflate(R.layout.fragment_step_fragment_example, container, false);
mRadioGroup = view.findViewById(R.id.radioGroupWrapper);
DatabaseHelper mDBHelper = new DatabaseHelper(getContext());
Log.d("#######", String.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)));
final List<Klasifikasi> lk = mDBHelper.getListKlasifikasiByGroup(String.valueOf(Integer.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) + 1));
final RadioButton[] rb = new RadioButton[lk.size()];
Log.d("##sz", "onCreateView: " + lk.size()) ;
int[][] states = new int[][] {
new int[] { android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
int[] colors = new int[] {
Color.BLACK,
Color.BLACK,
Color.BLACK,
Color.BLACK
};
mRadioButton1 = view.findViewById(R.id.checkbox1);
mRadioButton2 = view.findViewById(R.id.checkbox2);
mRadioButton3 = view.findViewById(R.id.checkbox3);
if(lk.size() > 0) {
Klasifikasi kl0 = lk.get(0);
Klasifikasi kl1 = lk.get(1);
Klasifikasi kl2 = lk.get(2);
mRadioButton1.setText(kl0.getKlasifikasi());
mRadioButton2.setText(kl1.getKlasifikasi());
mRadioButton3.setText(kl2.getKlasifikasi());
}
return view;
}
@Nullable
@Override
public VerificationError verifyStep() {
return null;
}
@Override
public void onSelected() {
}
@Override
public void onError(@NonNull VerificationError error) {
}
}
如何获取所选单选按钮的 ID?我的完整代码在这个链接中。
解决方案
用于mRadioGroup.getCheckedRadioButtonId()
获取RadioButton
内部选中的idRadioGroup
推荐阅读
- c - 使用 scanf() 函数的 C 编程中的意外输出
- javascript - 转换对象数组中的对象值?
- winforms - 当 TableLayoutPanel 激活滚动时,第一行变得太大
- c# - c# NLog如何只获取一个以longdate为文件名的日志文件
- jakarta-mail - 在邮件服务器中获取消息正文很少会导致错误的消息编号,因此返回不同消息的正文
- google-tag-manager - 对象 .... 在 GTM 变量中。如何?
- python - 每个键使用不同的值和每个键的不同颜色进行绘图
- git - 查找对当前分支引入某些更改的合并提交
- tkinter - tkinter:如何使用标签和条目在画布中显示任意大小的字典,并从输入输入操作字典
- python - Tensorflow XLA 编译器 - bazel 构建错误 - 找不到包含文件“.../hlo_ops_base.td”