EditText中没有输出。为什么?

时间:2016-01-09 12:59:13

标签: java android onclicklistener

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中没有给我任何输出。为什么会这样?代码没有错误,但仍然没有输出!

2 个答案:

答案 0 :(得分:2)

更改

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);
        }
    }
});

答案 1 :(得分:1)

您错误地使用了 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);
        }
    }
});