我有3个EditTexts - et1,et2,et3。我想将它们的总和放在另一个EditText,et33。
中如果总和为18,则et33的背景颜色为绿色;如果没有,它将是红色的。
我该怎么办?这就是我现在所拥有的:
int x = Integer.parseInt(et1.getText().toString());
int y = Integer.parseInt(et2.getText().toString());
int z = Integer.parseInt(et3.getText().toString());
et33.setText(String.valueOf(x+y+z));
if (Summ(x, y, z) = 18) {
//the background color et33 will be green
} else {
//the background color et33 will be red
}
public int Summ(int x, int y, int z) {
int sum = 0;
sum = x + y + z;
return sum;
}
答案 0 :(得分:0)
public class MainActivity extends AppCompatActivity {
EditText et1;
EditText et2;
EditText et3;
EditText et33;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et33 = (EditText) findViewById(R.id.et33);
et1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
int x = (et1.getText().toString().length() != 0) ? Integer.parseInt(et1.getText().toString()) : 0;
int y = (et2.getText().toString().length() != 0) ? Integer.parseInt(et2.getText().toString()) : 0;
int z = (et3.getText().toString().length() != 0) ? Integer.parseInt(et3.getText().toString()) : 0;
et33ColorChanged(x, y, z);
}
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
int x = (et1.getText().toString().length() != 0) ? Integer.parseInt(et1.getText().toString()) : 0;
int y = (et2.getText().toString().length() != 0) ? Integer.parseInt(et2.getText().toString()) : 0;
int z = (et3.getText().toString().length() != 0) ? Integer.parseInt(et3.getText().toString()) : 0;
et33ColorChanged(x, y, z);
}
});
et3.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
int x = (et1.getText().toString().length() != 0) ? Integer.parseInt(et1.getText().toString()) : 0;
int y = (et2.getText().toString().length() != 0) ? Integer.parseInt(et2.getText().toString()) : 0;
int z = (et3.getText().toString().length() != 0) ? Integer.parseInt(et3.getText().toString()) : 0;
et33ColorChanged(x, y, z);
}
});
}
public void et33ColorChanged(int x, int y, int z) {
et33.setText(String.valueOf(x + y + z));
if ((x + y + z) == 18) {
//the background color et33 will be green
et33.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
} else {
//the background color et33 will be red
et33.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
}
}
}
请注意,您的所有EditText
都应该只接受号码
android:inputType="number"
答案 1 :(得分:0)
我猜你的意思是:
if (Summ(x, y, z) == 18) {
et33.setBackgroundColor(Color.GREEN);
} else {
et33.setBackgroundColor(Color.RED);
}