Custom layout ListPreference

后端 未结 3 1603
深忆病人
深忆病人 2021-01-14 19:39

Prompt please where it is possible to read about creating a custom layout listpreference ( background and layout top panel, panel button ). Met - examples only for custom ro

3条回答
  •  北海茫月
    2021-01-14 19:59

    1. in your preference xml file

    2. CustomListPreference.java

      class CustomListPreference extends ListPreference {
      
      mListAdapter = new your_custom_list_adapter();
      private int mClickedDialogEntryIndex;
      public CustomListPreference(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      
      public CustomListPreference(Context context) {
          super(context);
      }
      
      @Override
      protected void onPrepareDialogBuilder(Builder builder) {
            mClickedDialogEntryIndex = findIndexOfValue(getValue());
            builder.setSingleChoiceItems(mListAdapter, mClickedDialogEntryIndex,
                  new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                          if (mClickedDialogEntryIndex != which) {
                              mClickedDialogEntryIndex = which;
                              CustomListPreference.this.notifyChanged();
                          }
                          CustomListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                          dialog.dismiss();
                      }
                  });
            builder.setPositiveButton(null, null);
      }
      
      @Override
      protected void onDialogClosed(boolean positiveResult) {
          CharSequence[] entryValues = getEntryValues();
          if (positiveResult && mClickedDialogEntryIndex >= 0 && entryValues != null) {
              String value = entryValues[mClickedDialogEntryIndex].toString();
              if (callChangeListener(value)) {
                  setValue(value);
              }
          }
      }
      
      }
      

提交回复
热议问题