How can I change the appearance of ListPreference Dialog

前端 未结 4 439
别跟我提以往
别跟我提以往 2021-01-13 10:02

I would like to change the RadioButton that appears on ListPreference dialog for a check mark or something different, or none, is there a simple wa

相关标签:
4条回答
  • 2021-01-13 10:36

    If you want to change the whole dialog, maybe to use a replacement dialog library like this material-dialogs package, you can use this replacement ListPreference:

    import com.afollestad.materialdialogs.MaterialDialog;
    
    public class MaterialListPreference extends ListPreference {
      private MaterialDialog.Builder mBuilder;
      private Context context;
    
      public MaterialListPreference(Context context) {
        super(context);
        this.context = context;
      }
    
      public MaterialListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
      }
    
      @Override
      protected void showDialog(Bundle state) {
        mBuilder = new MaterialDialog.Builder(context);
        mBuilder.title(getTitle());
        mBuilder.icon(getDialogIcon());
        mBuilder.positiveText(null);
        mBuilder.negativeText(getNegativeButtonText());
        mBuilder.items(getEntries());
        mBuilder.itemsCallback(new MaterialDialog.ListCallback() {
          @Override
          public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
            onClick(null, DialogInterface.BUTTON_POSITIVE);
            dialog.dismiss();
    
            if (which >= 0 && getEntryValues() != null) {
              String value = getEntryValues()[which].toString();
              if (callChangeListener(value))
                setValue(value);
            }
          }
        });
    
        final View contentView = onCreateDialogView();
        if (contentView != null) {
          onBindDialogView(contentView);
          mBuilder.customView(contentView);
        }
        else
          mBuilder.content(getDialogMessage());
    
        mBuilder.show();
      }
    
    }
    

    It doesn't do much, just the bare minimum to override the dialog display and selection callback parts. YMMV very slightly if you opt for a different dialog library but not too much, they tend to be more or less direct replacements for AlertDialog.

    0 讨论(0)
  • 2021-01-13 10:49

    After analyzing the source code of ListPreference in AndroidX I came to the conclusion that the simplest way is to write a Preference that inherits from ListPreference but generates it's own AlertDialog that you can customize in whatever way you want:

    import android.app.AlertDialog
    import android.content.Context
    import android.util.AttributeSet
    import androidx.preference.ListPreference
    import com.my.app.R
    
    class MyListPreference : ListPreference {
    
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
            context,
            attrs,
            defStyleAttr
        )
    
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    
        constructor(context: Context?) : super(context)
    
        override fun onClick() {
    
            AlertDialog.Builder(context).setSingleChoiceItems(R.array.app_language, getValueIndex())
            { dialog, index->
                if(callChangeListener(entryValues[index].toString())){
                    setValueIndex(index)
                }
                dialog.dismiss()
            }
                .setNegativeButton(R.string.cancel) { dialog, _  -> dialog.dismiss() }
                .show()
        }
    
        private fun getValueIndex() = context.resources.getStringArray(R.array.app_language).indexOf(value)
    }
    

    That way you can use your own style, title, icon and whatever else you can configure on an AlertDialog.

    0 讨论(0)
  • 2021-01-13 10:54

    Try the following links hope it may help you out

    http://developer.android.com/guide/topics/ui/settings.html

    example2

    example3

    example4

    0 讨论(0)
  • 2021-01-13 10:55

    if you want java code. try to following code.

    public class SearchEngineListPreference extends ListPreference {
        private Context mContext;
        public SearchEngineListPreference(Context context) {
            super(context);
            mContext = context;
        }
    
        public SearchEngineListPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
        }
    
        @SuppressWarnings("New API")
        public SearchEngineListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            mContext = context;
        }
    
        @SuppressWarnings("New API")
        public SearchEngineListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            mContext = context;
        }
    
        @Override
        protected void onClick() {
            AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
            ab.setCancelable(true);
            ab.setTitle(getTitle());
            String value = getValue();
            ab.setSingleChoiceItems(R.array.setting_entries_search_engine,
                    findIndexOfValue(value),
                    (dialog, index) -> {
                        CharSequence[] entryValues = getEntryValues();
                        if (callChangeListener(entryValues[index].toString())){
                            setValueIndex(index);
                        }
                        dialog.dismiss();
                    }).setNeutralButton(mContext.getString(R.string.dialog_button_custom),
                    (dialog, whichButton) -> {
                        dialog.dismiss();
                        showEditDialog();
                    }).setNegativeButton(mContext.getString(R.string.dialog_button_negative),
                    (dialog, whichButton) -> {
                        dialog.dismiss();
                    });
            ab.show();
        }
    
        private void showEditDialog() {
          .......
        }
    
    
    0 讨论(0)
提交回复
热议问题