gpt4 book ai didi

android - 无法在单选组中显示按钮的值

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:23 24 4
gpt4 key购买 nike

我有下面的简单代码,但我不明白为什么它对我不起作用。我只是想做的是根据我点击的单选按钮显示( toast )“男性”或“女性”。

这是主要 Activity 的代码:

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>

有人可以指出我遗漏了什么吗?这令人沮丧。

最佳答案

您的代码必须如下所示:

public class FormActivity extends Activity implements View.OnClickListener {

private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
// initialize component here like this
radio_g = (RadioGroup)findViewById(R.id.genderButton);
button_sbm = (Button)findViewById(R.id.getBMI);

// Then call listener on button click like this
button_sbm .setOnClickListener(this);
}

@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();
}

试试这个它工作正常我试试。

关于android - 无法在单选组中显示按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com