我有下面的簡單代碼,我不明白爲什麼它不適用於我。 我只是想根據我點擊哪個單選按鈕來顯示(烤麪包)「男性」或「女性」。無法顯示廣播組中按鈕的值
以下是主要活動代碼:
private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;
public void onClickListenerButton() {
radio_g = (RadioGroup)findViewById(R.id.genderButton);
button_sbm = (Button)findViewById(R.id.getBMI);
button_sbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton)findViewById(selected_id);
Toast.makeText(MainActivity.this,
radio_b.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
);
}
和無線電組:
<RadioGroup
android:id="@+id/genderButton"
android:layout_width="124dp"
android:layout_height="67dp"
android:layout_marginLeft="89dp"
android:layout_marginStart="88dp"
android:layout_marginTop="63dp"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ageField"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Male"
tools:text="male" />
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
tools:text="female" />
</RadioGroup>
有人可以,請指出我錯過了什麼?這令人沮喪。
一切似乎從您發佈的代碼沒關係,你可能需要添加一些更多的情況下,是onClickListenerButton()被調用? –
@BradleyWilson,不,這幾乎是我所有的代碼。我不叫它,我不知道我知道如何。 –
好的,不用擔心。我現在寫一個小例子。 –