【问题标题】:how to add the value of two radio button?如何添加两个单选按钮的值?
【发布时间】:2018-06-08 10:19:04
【问题描述】:

我有两个radioGroup,每个都包含不确定数量的radioButton 我想将radioGroup 1 中选择的radioButton 的值与radioGroup 2 中选择的radioButton 的值相加,并在toast 中显示结果。我尝试下面的代码,但它似乎不起作用

请问我该怎么做?

我的尝试:

RadioButton lamp1;
RadioButton tele1;
RadioGroup group, group2;
Button button;
int result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_type_installation);

    addListenerOnButton();
}

public void addListenerOnButton() {

    group = findViewById(R.id.group);
    group2 = findViewById(R.id.group2);

    ampoule1 = findViewById(R.id.lamp1);
    tele1 = findViewById(R.id.tele1);

    button = findViewById(R.id.calcul);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int selectedId = group.getCheckedRadioButtonId();
            int selectedId1 = group2.getCheckedRadioButtonId();

            ampoule1 = findViewById(selectedId);
            tele1 = findViewById(selectedId1);

            result = selectedId+selectedId1;
            Toast.makeText(Type_Installation.this, "your consommation is"
                    +result+"W", Toast.LENGTH_SHORT).show();
        }
    });
}

XML 代码:

<TextView
    android:id="@+id/ampoule"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/appBar"
    android:padding="10dp"
    android:layout_marginStart="40dp"
    android:layout_marginLeft="40dp"
    android:text="Ampoule"
    android:textSize="20sp"/>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/ampoule"
    android:background="@color/material_blue_grey_700"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginEnd="20dp"/>

<RadioGroup
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ampoule"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp">

    <RadioButton
        android:id="@+id/lamp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="40 W"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/lamp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="60 W"
        android:textColor="@color/material_blue_grey_700"/>

    <RadioButton
        android:id="@+id/lamp3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="75 W"
        android:textColor="@color/material_blue_grey_700"/>

    <RadioButton
        android:id="@+id/ampoule4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100 W"
        android:textColor="@color/material_blue_grey_700"/>
</RadioGroup>

<TextView
    android:id="@+id/tele"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/group"
    android:padding="10dp"
    android:layout_marginStart="40dp"
    android:layout_marginLeft="40dp"
    android:text="Televiseur"
    android:textSize="20sp"/>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/tele"
    android:background="@color/material_blue_grey_700"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginEnd="20dp"/>

<RadioGroup
    android:id="@+id/group2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tele"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp">

    <RadioButton
        android:id="@+id/tele1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100 W"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/tele2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="200 W"
        android:textColor="@color/material_blue_grey_700"/>

    <RadioButton
        android:id="@+id/tele3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="300 W"
        android:textColor="@color/material_blue_grey_700"/>
</RadioGroup>

<Button
    android:id="@+id/calcul"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="20dp"
    android:background="@drawable/material_button1"
    android:text="calculer"/>

【问题讨论】:

  • 您希望在选择其中一个单选按钮时显示 toast
  • 当我点击“计算”按钮时没有
  • 检查我的答案

标签: android radio-group


【解决方案1】:

使用了这几行代码

 lamp1 = findViewById(selectedId);
  tele1 = findViewById(selectedId1);
  result = Integer.valueOf(lamp1.getText().toString())+Integer.valueOf(tele1.getText().toString());

不是这个

ampoule1 = findViewById(selectedId);
tele1 = findViewById(selectedId1);
 result = selectedId+selectedId1;

【讨论】:

  • 请注意,您的文本不是纯整数。我会让你的单选按钮只有整数的文本值。然后这将工作
  • 感谢指正。我认为单选按钮的值是整数,因为他在 Toast Toast.makeText(Type_Installation.this, "your consommation is" +result+"W", Toast.LENGTH_SHORT).show(); 中添加 String 值;
【解决方案2】:

你可以先从电台组中获取选中的radio button的id,然后获取id后,你可以得到选中的radio button的文本,然后你可以添加它们或者做任何你想做的事情。

以下代码假定您的 radio buttons 具有整数值作为文本 i.e 5,6,7

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // get selected radio button from radioGroup
        int selectedId1 = group.getCheckedRadioButtonId();
        int selectedId2 = group2.getCheckedRadioButtonId();

        // find the radiobutton by returned id
        RadioButton radioButton1 = (RadioButton) findViewById(selectedId1);
        RadioButton radioButton2 = (RadioButton) findViewById(selectedId2);


        //Assuming text on radio buttons are integer values like 5.6.7 etc

        int result = Integer.parseInt(radioButton1.getText())+Integer.parseInt(radioButton2 .getText());


        Toast.makeText(MyAndroidAppActivity.this,
            "Resulkt"+result, Toast.LENGTH_SHORT).show();
    }
});

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2014-09-10
    • 2018-06-01
    • 1970-01-01
    相关资源
    最近更新 更多