简体   繁体   中英

How can I change the appearance of ListPreference Dialog

我想更改出现在ListPreference对话框中的RadioButton是否有一个复选标记或不同的东西,或者没有,有没有一种简单的方法可以做到这一点?

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 .

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 .

Try the following links hope it may help you out

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

Example

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() {
      .......
    }

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