简体   繁体   中英

CheckBox isSelected() method always returns false

I am working on an Android application where I have to check the CheckBox selection onClick() so I made this

                if (checkBoxRM.isSelected() == true) {
                isSelectedValue = "True";
                //debug
                Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
            } else {
                //debug
                isSelectedValue = "False";
                Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
            }

But seems like isSelectedValue is always false .

而不是isSelected()执行isChecked()

You have to cast the checkBox to enable isChecked()

This works:

boolean selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked();

This not (here, you can only choose "isSelected"):

boolean selected = view.findViewById(R.id.main_cbSelectAll).isChecked();

If you want to use it as an SQLite-statement, you have to transform to an integer:

int selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked() ? 1 : 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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