Java 将togglebutton布尔值转换为整数值,然后在textview中相加
我想使用四个切换按钮(最终是八、十二或十六),并根据切换到的位置为每个按钮指定一个值 因此,Java 将togglebutton布尔值转换为整数值,然后在textview中相加,java,android,int,boolean,togglebutton,Java,Android,Int,Boolean,Togglebutton,我想使用四个切换按钮(最终是八、十二或十六),并根据切换到的位置为每个按钮指定一个值 因此,toggle1将给出一个1或0的值,toggle2将给出一个2或0的值(tb4或0、tb8或0等) 然后我想将所有按钮的当前值加在一起,并显示在文本视图中 我刚刚开始使用前2个,但不确定如何将这些值输入到displayDecimalAnswer方法中。我显然遗漏了一些非常明显的东西,因为它听起来很简单 帮助我欧比-万·克诺比斯你是我唯一的希望 太棒了,谢谢,效果很好。将在任务单中添加其他按钮。但要澄清:1
toggle1
将给出一个1或0的值,toggle2
将给出一个2或0的值(tb4或0、tb8或0等)
然后我想将所有按钮的当前值加在一起,并显示在文本视图中
我刚刚开始使用前2个,但不确定如何将这些值输入到displayDecimalAnswer
方法中。我显然遗漏了一些非常明显的东西,因为它听起来很简单
帮助我欧比-万·克诺比斯你是我唯一的希望
太棒了,谢谢,效果很好。将在任务单中添加其他按钮。但要澄清:1:您将displayDecimalAnswer添加到onCheckedChanged方法。2:在displayDecimalAnswer方法中移动整数,因为它们是类级别的。和3:将setText更改为包含空引号。那是干什么用的。由于您为toggle实现了change listner,因此它将是更新您的值的最佳位置。2.首先,如果可能的话,您应该尝试使用局部变量,一旦方法完成工作,这些局部变量就会被破坏。您有类级别的布尔值,因此它在使int变量成为全局变量时使用较少。3.我懒得写(String.valueOf):)它会很好用的。你是个超级明星。我已经为这个(我的第一个应用)工作了两周了,终于有8个按钮可以工作了。现在需要使它看起来漂亮,并添加更多功能:D
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);
}