无法在广播组中显示按钮的值

时间:2017-04-15 11:21:55

标签: android

我有下面的简单代码,我无法理解为什么它对我不起作用。 我只想根据我点击的单选按钮显示(吐司)“男性”或“女性”。

以下是主要活动的代码:

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>

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

4 个答案:

答案 0 :(得分:1)

检查this它也类似..

onCreate()来电

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    onClickListenerButton();  //add this

  }

答案 1 :(得分:1)

这是完整类应该是什么样子的一个示例,您需要对Activity life-cycle进行一些研究,以了解在特定时间调用哪些方法。然后,您将了解更多活动的概念以及如何呈现它。

-p

注意:我是从头顶写的,我还没有测试过。

答案 2 :(得分:1)

您的代码必须如下所示:

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

试试这个它可以正常使用。

答案 3 :(得分:0)

这是您在活动结构中的代码

public class radioToButton extends AppCompatActivity {

    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_radio_to_button);

        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(radioToButton.this,
                                radio_b.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }

和XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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"
        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>

    <Button
        android:layout_below="@id/genderButton"
        android:text="AAAAAAAA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/getBMI"/>
</RelativeLayout>