繁体   English   中英

Android如何在点击CheckBoxPreference时显示弹出窗口

[英]Android how to show pop up on clicking CheckBoxPreference

我有一个PreferenceScreen,其中我有一个包含以下代码的复选框:

<CheckBoxPreference
        android:defaultValue="false"
        android:key="the_key"
        android:summary="Random text here."
        android:title="My Title"
        />

我希望在单击此复选框时显示弹出消息,该复选框将具有设置按钮和取消。 如果将按下“设置”,则复选框将被检查,如果以前未选中,则相反。 我怎样才能做到这一点? 这是我的弹出消息的代码。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Some message that will be displayed")
            .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do something
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}

另外,我如何获得复选框的值(已选中或未选中)? 谢谢!!

要从CheckBoxPreference检查是否获取复选框,请使用此选项

CheckBoxPreference pref = (CheckBoxPreference) findPreference("checkbox_preference");
pref.isChecked(); //returns if it is checked or not

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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