简体   繁体   中英

Using a dialog preference with custom layout

I have a custom layout that I am using with a dialog preference, and the layout appears fine in the dialog box but the problem is that I have check boxes in the layout, and checking them doesnt make them behave as expected. Below is the code of my DialogPreferences.java class:

@Override
protected void onDialogClosed(boolean positiveResult) {
    LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
    View view = inflater.inflate(R.layout.imagechoice, null);
    button1 = (CheckBox)view.findViewById(R.id.background1);
    button2 = (CheckBox)view.findViewById(R.id.background2);
    button3 = (CheckBox)view.findViewById(R.id.background3);
    button4 = (CheckBox)view.findViewById(R.id.background4);

    button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            selected=1;
            button2.setChecked(false);
            button3.setChecked(false);
            button4.setChecked(false);
        }
    });
    button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            selected=2;
            button1.setChecked(false);
            button3.setChecked(false);
            button4.setChecked(false);
        }
    });
    if(positiveResult){
        Log.d("BACKGROUND onDialogClosed", Integer.toString(selected));
        persistInt(selected);
    }

    super.onDialogClosed(positiveResult);    //To change body of overridden methods use File | Settings | File Templates.
}

When the app is running and I have the log open, I tried selecting the first or second check and I always see the value 0 printed by the log statement, indicating that the variable selected is never being updated as expected. I have tried to put the check box code in the

public DialogPreferences(Context oContext, AttributeSet attrs)
{...}

method and that led to the same results. In which method of this file is the proper place to put the code needed to do something when the check boxes are checked?

You should override onCreateDialogView and put your onCheckChangeListeners in there. The problem is currently they are only listening when onDialogClosed is called which is after you are trying check them.

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