如何将复选框的值转换为整数总和?

huangapple go评论66阅读模式
英文:

How to convert the values of CheckBoxes into a sum of integers?

问题

我创建了3个包含颜色的CheckBox:红色、蓝色和白色。每个复选框都包含一个值。红色复选框包含值10。蓝色复选框包含值20。白色复选框包含值30。
当任何用户点击任何CheckBox,然后按下calculateResult按钮时,名为showResult的textView将显示用户点击的CheckBox值的总和。

我的代码:

package com.mycode.testingcheckbox;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    CheckBox red;
    CheckBox blue;
    CheckBox white;
    Button calculateResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        red = findViewById(R.id.red);
        blue = findViewById(R.id.blue);
        white = findViewById(R.id.white);
        calculateResult = findViewById(R.id.calculateResult);

        final TextView showResult = findViewById(R.id.showResult);

        calculateResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int result = 0;

                if (red.isChecked()) {
                    result += 10;
                }

                if (blue.isChecked()) {
                    result += 20;
                }

                if (white.isChecked()) {
                    result += 30;
                }

                showResult.setText(result);

            }
        });
    }
}
英文:

I created 3 CheckBox containing the colors: red, blue and white. Each checkbox contains a value. The red checkbox contains the value 10. The blue checkbox contains the value 20. And the white checkbox contains the value 30.
I would like that when any user clicked on any CheckBox and then pressed the calculateResult button, the textView called showResult would show the sum of the CheckBox values ​​clicked by the user.

My code:


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
CheckBox red;
CheckBox blue;
CheckBox white;
Button calculateResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = findViewById(R.id.red);
blue = findViewById(R.id.blue);
white = findViewById(R.id.white);
calculateResult = findViewById(R.id.calculateResult);
final TextView showResult = findViewById(R.id.showResult);
calculateResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result = 0;
if (red.isChecked()) {
result += 10;
}
if (blue.isChecked()) {
result += 20;
}
if (white.isChecked()) {
result += 30;
}
showResult.setText(result);
}
});
}
}```
</details>
# 答案1
**得分**: 1
这是您想要的:
```java
public class MainActivity extends AppCompatActivity {
CheckBox red;
CheckBox blue;
CheckBox white;
Button calculateResult;
Integer[] checkBoxValues = {10, 20, 30};
int result = 0;
int redValue = 0, blueValue = 0, whiteValue = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = findViewById(R.id.red);
blue = findViewById(R.id.blue);
white = findViewById(R.id.white);
calculateResult = findViewById(R.id.calculateResult);
final TextView showResult = findViewById(R.id.showResult);
calculateResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (red.isChecked()) redValue = checkBoxValues[0];
else redValue = 0;
if (blue.isChecked()) blueValue = checkBoxValues[1];
else blueValue = 0;
if (white.isChecked()) whiteValue = checkBoxValues[2];
else whiteValue = 0;
result = redValue + blueValue + whiteValue;
showResult.setText(String.valueOf(result));
}
});
}
}
英文:

Here's what you want :

public class MainActivity extends AppCompatActivity {
CheckBox red;
CheckBox blue;
CheckBox white;
Button calculateResult;
Integer[] checkBoxValues = {10, 20, 30};
int result = 0;
int redValue = 0, blueValue = 0, whiteValue = 0;
@SuppressLint(&quot;ClickableViewAccessibility&quot;)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = findViewById(R.id.red);
blue = findViewById(R.id.blue);
white = findViewById(R.id.white);
calculateResult = findViewById(R.id.calculateResult);
final TextView showResult = findViewById(R.id.showResult);
calculateResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (red.isChecked()) redValue = checkBoxValues[0];
else redValue = 0;
if (blue.isChecked()) blueValue = checkBoxValues[1];
else blueValue = 0;
if (white.isChecked()) whiteValue = checkBoxValues[2];
else whiteValue = 0;
result = redValue + blueValue + whiteValue;
showResult.setText(String.valueOf(result));
}
});
}
}

huangapple
  • 本文由 发表于 2020年9月22日 04:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63999783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定