protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
CheckBox yes = (CheckBox) findViewById(R.id.yes);
CheckBox no = (CheckBox) findViewById(R.id.no);
EditText ageText = (EditText) findViewById(R.id.ageText);
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText3 = (EditText) findViewById(R.id.editText3);
int age;
age = 0;
if (yes.isChecked()) {
editText3.setText(age + 15);
} else if (no.isChecked()) {
editText3.setText(age - 10);
}
finish();
}
});
}
这段代码没有在editText3中提供任何输出。为什么会这样呢?该代码没有错误,但仍然没有输出!
最佳答案
您错误地使用了setOnClickListener。以此方式更改代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) view).isChecked();
if (checkBox.isChecked()) {
//check age is string or not.if not a string means change into String format
editText3.setText(age + 15);
} else if (checkBox.isChecked()) {
editText3.setText(age - 10);
}
}
});