将 togglebutton 布尔值转换为整数值,然后在文本视图中加在一起

Converting togglebutton boolean to integer values then add together in a textview

我想使用四个切换按钮(最终是八个、十二个或十六个),并根据切换到的位置为每个切换按钮赋予一个值。

因此 toggle1 将给出一个值 1 或 0,toggle2 将给出一个值 2 或 0(tb4 4 或 0,tb8 8 或 0 等)

然后我想将所有按钮的当前值加在一起并在文本视图中显示。

我刚开始使用前 2 个值,但不确定如何将这些值输入 displayDecimalAnswer 方法。我显然在这里遗漏了一些非常明显的东西,因为它听起来很简单。

帮助我欧比万克诺比斯,你是我唯一的希望。

package com.example.android.binary04;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;


import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

ToggleButton toggle1;
ToggleButton toggle2;
ToggleButton toggle4;
ToggleButton toggle8;
TextView decimalAnswer;

int totalValues;
boolean toggle1Status;
boolean toggle2Status;
boolean toggle4Status;
boolean toggle8Status;

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


    toggle1 = (ToggleButton) findViewById(R.id.toggle1);
    toggle2 = (ToggleButton) findViewById(R.id.toggle2);
    toggle4 = (ToggleButton) findViewById(R.id.toggle4);
    toggle8 = (ToggleButton) findViewById(R.id.toggle8);
    toggle1.setOnCheckedChangeListener(this);
    toggle2.setOnCheckedChangeListener(this);
    toggle4.setOnCheckedChangeListener(this);
    toggle8.setOnCheckedChangeListener(this);
    decimalAnswer = (TextView) findViewById(R.id.decimal);
}


@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton == toggle1) {
        if (isChecked) {
            toggle1Status = true;
        } else {
            toggle1Status = false;
        }
    } else if (compoundButton == toggle2) {
        if (isChecked) {
            toggle2Status = true;
        } else {
            toggle2Status = false;
        }
    }

}
/**
* Here I wanted to give a int value to boolean whether each togglebutton
* is clicked or not. Then add the value of each button together
*/

int valueOfOnes = (toggle1Status) ? 1 : 0;
int valueOfTwos = (toggle2Status) ? 2 : 0;
int answer = valueOfOnes + valueOfTwos;



/**
 * Displays decimal answer.
 */
public void displayDecimalAnswer(int answer) {
    TextView decimalView = (TextView) findViewById(R.id.decimal);
    decimalView.setText(String.valueOf(answer));
}

}
     @Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton == toggle1) {
        if (isChecked) {
            toggle1Status = true;
        } else { 
            toggle1Status = false;
        } 
    } else if (compoundButton == toggle2) {
        if (isChecked) {
            toggle2Status = true;
        } else { 
            toggle2Status = false;
        } 
    } 
 displayDecimalAnswer();
} 

    /** 
     * Displays decimal answer. 
     */ 
    public void displayDecimalAnswer() {
       int valueOfOnes = (toggle1Status) ? 1 : 0;//Since toggle1Status is class level variable it will be accessibible
       int valueOfTwos = (toggle2Status) ? 2 : 0;
       int answer = valueOfOnes + valueOfTwos;

        TextView decimalView = (TextView) findViewById(R.id.decimal);
        decimalView.setText(""+answer);
    }