mercredi 28 février 2018

how to get checked of checkbox from other class?

I have a checkbox in Setting.class:

Checkbox checkBoxFlash = findViewById(R.id.checkBoxFlash);
if(checkBoxFlash.isChecked()) {
        checkBoxFlash.setChecked(mPrefe.getBoolean(KEY_CHECKED_FLASH, true));
    }else{
        checkBoxFlash.setChecked(mPrefe.getBoolean(KEY_CHECKED_FLASH, false));
    }

checkBoxFlash.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(compoundButton.isChecked()){
                myEditor.putBoolean(KEY_CHECKED_FLASH, true);
                myEditor.apply();
            }else{
                myEditor.putBoolean(KEY_CHECKED_FLASH, false);
                myEditor.apply();
            }
        }
    });

and I have a ImageButton in Main.class:

ImageButton imgStart = findViewById(R.id.imageButtonStart);
imgStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean checked = ((CheckBox) view).isChecked(); //error in here

            switch (view.getId()) {
                case R.id.checkBoxFlash:
                    try {

                        mCameraId = mCameraManager.getCameraIdList()[0];
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                    if (checked) {
                        turnOnFlash();
                    } else {
                        turnOffFlash();
                    }
                    break;

I got an error in

                boolean checked = ((CheckBox) view).isChecked();
// java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.CheckBox

Why can not I declare it? and Can I know how to check it in Main.class? My purpose is click ImageButton it will perform function of checkbox! Thanks for a questions! (sorry if i'm wrong english grammar)




Aucun commentaire:

Enregistrer un commentaire