У меня есть радиогруппа, состоящая из трех радиокнопок 'yes'
, 'no'
, 'do not know'
. Я устанавливаю android:checkedButton="@id/r3"
по умолчанию, выберите третью радиокнопку. Я установил setOnCheckedChangeListener
для радиогруппы, и она вызывается при выборе любой радиокнопки из пользовательского интерфейса.
Я хочу вызвать слушателя на onCreate
для выбранной по умолчанию радиокнопки, потому что я выполняю установку некоторых значений в коде по умолчанию.
Есть ли возможность получить выбранную по умолчанию радиокнопку в радиогруппе в onCreate
и выполнить некоторые действия, позже setOnCheckedChangeListener
будет обрабатывать выбранную радиокнопку из пользовательского интерфейса
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@id/r3"
android:orientation="horizontal"
android:weightSum="3">
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes"
android:textColor="@color/fontBlackEnable" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No"
android:textColor="@color/fontBlackEnable" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/do_not_know" />
</RadioGroup>
public class MyFragment extends Fragment {
View rootView;
Context context;
RadioGroup mRadioAns3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = inflater.inflate(R.layout.my_fragment, container, false);
context = container.getContext();
mRadioAns3 = rootView.findViewById(R.id.rg1);
//Todo: get default selected radiobutton and show toast on load of fragment
mRadioAns3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(context,((RadioButton) rootView.findViewById(checkedId)).getText() ,Toast.LENGTH_LONG ).show();
}
});
return rootView;
}
}