这是RadioGroup xml文件:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radio_group"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</RadioGroup>
然后我编写了按钮:
public class AbilityDialog extends DialogFragment {
public static final String ATAG = "ABILITY_DIALOG";
private String[] mAbilityList;
public AbilityDialog() {
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return inflater.inflate(R.layout.dialog_ability, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
RadioGroup radioGroup = (RadioGroup) getView().findViewById(R.id.radio_group);
radioGroup.removeAllViews();
mAbilityList = getArguments().getStringArray("AbilityList");
int selectedAbility = getArguments().getInt("SelectedAbility");
for (int i=0; i < mAbilityList.length; i++) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(mAbilityList[i]);
radioButton.setPadding(0, 0, 48, 0);
radioGroup.addView(radioButton);
}
radioGroup.check(selectedAbility);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentManager fm = getActivity().getSupportFragmentManager();
MyFragment myFragment = (MyFragment) fm.findFragmentByTag(MyFragment.TAG);
myFragment.setAbility(checkedId-1);
getDialog().dismiss();
}
});
}
}
这是主要片段:
public class MyFragment extends DialogFragment {
public final static String TAG = "MAIN_FRAGMENT";
private int mAbility;
private String[] mAbilityList;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
TextView ability = (TextView) view.findViewById(R.id.stats_abilities);
mAbilityList = getResources().getStringArray(R.array.p184Ability);
String abilities = mAbilityList[0];
for (int i=1; i < mAbilityList.length; i++) {
abilities += " / " + mAbilityList[i];
}
if (getArguments() != null && getArguments().getInt("Ability") != 0) {
setAbility(getArguments().getInt("Ability") - 1);
} else {
mAbility = -1;
}
ability.setText(abilities);
ability.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
AbilityDialog abilityDialog = new AbilityDialog();
Bundle bundle = new Bundle();
bundle.putStringArray("AbilityList", mAbilityList);
bundle.putInt("SelectedAbility", mAbility);
abilityDialog.setArguments(bundle);
abilityDialog.show(fm, AbilityDialog.ATAG);
}
});
public void setAbility(int ability) {
mAbility = ability;
if (mAbility != -1) {
TextView pokemonAbility = (TextView) getView().findViewById(R.id.stats_abilities);
pokemonAbility.setText(mAbilityList[ability]);
}
}
private void closeFragment() {
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}
我希望现在有足够的信息。
编辑:它似乎不可编译。如果问题仍然无法诊断,我会提出更多代码。
答案 0 :(得分:0)
我的第一个猜测是,虽然我没有办法在没有更多代码的情况下检查它,但是视图没有被重新创建,因此您正在向组中添加越来越多的单选按钮,而不是每次重新启动它。
您可以强制它使用radioGroup.removeAllViews()
将其添加到onViewCreated(...)
的开头,看看会发生什么。