6

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

4

4 回答 4

14

如果你想改变整个对话框,也许要使用像这个材料对话框包这样的替换对话框库,你可以使用这个替换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();
  }

}

它没有做太多,只是覆盖对话框显示和选择回调部分的最低限度。如果您选择不同的对话框库但不是太多,YMMV 会非常轻微,它们往往或多或少直接替代AlertDialog.

于 2014-12-11T18:29:54.847 回答
5

在分析了ListPreferenceAndroidX 中的源代码后,我得出的结论是,最简单的方法是编写一个Preference继承自ListPreference但生成它自己的AlertDialog,您可以按照您想要的任何方式自定义:

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)
}

这样您就可以使用自己的样式、标题、图标以及您可以在AlertDialog.

于 2019-08-04T16:02:36.803 回答
2

试试下面的链接希望它可以帮助你

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

例子

于 2013-01-18T12:51:53.097 回答
0

如果你想要java代码。尝试以下代码。

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

于 2020-08-30T11:09:11.693 回答