[英]No output in EditText. Why?
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中提供任何輸出。 為什么會這樣呢? 該代碼沒有錯誤,但仍然沒有輸出!
更改
button.setOnClickListener(new)onClickListener() ...
至
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) view).isChecked();
if (checkBox.isChecked()){
editText3.setText(age+15);
} else if (checkBox.isChecked()){
editText3.setText(age - 10);
}
}
});
您錯誤地使用了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);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.