我想使用四个togglebuttons(最终是八个,十二个或十六个),并根据它切换到的位置为每个提供一个值。
因此toggle1
将给出1或0的值,toggle2
将给出2或0的值(tb4 4或0,tb8 8或0等)
然后我想将所有按钮的当前值添加到一起并显示在文本视图中。
我刚刚开始使用前2个,但我不确定如何将这些值添加到displayDecimalAnswer
方法中。我显然遗漏了一些非常明显的东西,因为它听起来很简单。
帮助我Obi Wan Kenobis你是我唯一的希望。
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));
}
}
答案 0 :(得分:2)
@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);
}