0

次のコードのチェックボックスがある 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();
}

また、チェックボックスの値(チェックされているかどうか)を取得するにはどうすればよいですか? ありがとう!!

4

1 に答える 1

0

チェックボックスがチェックされているかどうかを CheckBoxPreference から取得するには、これを使用します

CheckBoxPreference pref = (CheckBoxPreference) findPreference("checkbox_preference");
pref.isChecked(); //returns if it is checked or not
于 2015-02-24T14:20:56.437 に答える