在xml布局中,我有RadioGroup,Button1,Button2.当用户点击button1时,在RadioGroup中以编程方式创建了几个单选按钮(单选按钮的总数可能不同(pocet =要创建的单选按钮的数量).
final RadioButton[] rb = new RadioButton[pocet];
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
radiobuttonCount++;
for(int i=0; i<pocet; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Radio Button " + radiobuttonCount);
rb[i].setId(radiobuttonCount+i);
rb[i].setBackgroundResource(R.drawable.button_green);
rg.addView(rb[i]);
}
Run Code Online (Sandbox Code Playgroud)
我尝试做的是:当用户从RadioGroup中选择xy项时,我会将所选值传递给textview并删除所有radioButtons.
为了删除目的,我使用:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,这有时效果很好,但有时第一个单选按钮仍未删除.
PS稍后我想添加button2,它将为radiogroup提供不同的项目和不同的单选按钮数量.这就是我需要在用户选择后删除所有单选按钮的原因.
在我的申请中,我有3个活动.第一个 - 我开始活动#2的主要内容.从#2开始我开始#3,同时我完成#2.当我完成#3时,我会自动回到#1.问题:如何从3回到1时添加/运行代码?
不确定它是否有意义.但我想要做的是,当结束#3并回到#1时,我想检查文件xyz是否存在并基于它来改变活动#1中的UI.
#1中的OnResume没有运行,从不运行.(对于第一个活动,可能系统不会暂停运行)
如果只有活动#1和2,我可以使用startActivityForResult.但是有两项活动没有做我需要的......