简体   繁体   English

在EditText中没有输出。 为什么?

[英]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();
        }
    });
}

This code is not giving me any output in editText3. 这段代码没有在editText3中提供任何输出。 Why this is happening? 为什么会这样呢? The code has no bugs but still no output! 该代码没有错误,但仍然没有输出!

Change 更改

button.setOnClickListener(new)onClickListener() ...

to

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

You wrongly used setOnClickListener . 您错误地使用了setOnClickListener Change your code this way: 通过以下方式更改代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM