简体   繁体   English

使用listpreference并获取密钥有效,但没有确定按钮

[英]Using listpreference and getting the key works but no ok button

I'm using listpreference in my android app and getting my key values and all is well and works good (now that you guys have helped me) BUT - when my listpreference menus popup, they only contain a cancel button. 我在我的Android应用程序中使用listpreference并获取我的键值,一切都很好并且运行良好(现在你们帮助了我)但是 - 当我的listpreference菜单弹出时,它们只包含一个取消按钮。

Let's say the user is choosing between red, blue, and green. 假设用户在红色,蓝色和绿色之间进行选择。 When the listpreference dialog first pops-up, the dialog only shows a cancel button. 首次弹出列表首选项对话框时,对话框仅显示取消按钮。 Because of that, the dialog disappears as soon as the user selects their choice. 因此,只要用户选择了对话框,对话框就会消失。 I would like it so that when the user chooses their setting, they see the radio button get highlighted and then they go ahead and click the ok button...but I don't have an ok button and can't figure out why. 我希望这样当用户选择他们的设置时,他们会看到单选按钮突出显示然后他们继续并单击确定按钮...但我没有确定按钮而无法弄清楚原因。 Any help would be awesome...al 任何帮助都会很棒......

I have done what the previous answer suggested and implemented my own ListPreference based on Android source code. 我已经完成了之前的答案建议并基于Android源代码实现了我自己的ListPreference。 Below is my implementation that adds the OK button. 下面是我添加OK按钮的实现。

myPreferenceList.java myPreferenceList.java

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.preference.ListPreference;
import android.util.AttributeSet;


public class myPreferenceList extends ListPreference implements OnClickListener{

    private int mClickedDialogEntryIndex;

    public myPreferenceList(Context context, AttributeSet attrs) {
        super(context, attrs);


    }

    public myPreferenceList(Context context) {
        this(context, null);
    }

    private int getValueIndex() {
        return findIndexOfValue(this.getValue() +"");
    }


    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);

        mClickedDialogEntryIndex = getValueIndex();
        builder.setSingleChoiceItems(this.getEntries(), mClickedDialogEntryIndex, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                mClickedDialogEntryIndex = which;

            }
        });

        System.out.println(getEntry() + " " + this.getEntries()[0]);
        builder.setPositiveButton("OK", this);
    }

    public  void onClick (DialogInterface dialog, int which)
    {
        this.setValue(this.getEntryValues()[mClickedDialogEntryIndex]+"");
    }

}

Then you can use the class in your preference.xml as follow: 然后,您可以在preference.xml中使用该类,如下所示:

<com.yourApplicationName.myPreferenceList
            android:key="yourKey"
            android:entries="@array/yourEntries"
            android:entryValues="@array/yourValues"
            android:title="@string/yourTitle" />

You can clone and reimplement ListPreference to work the way you want, making your own custom Preference class as a result. 您可以克隆并重新实现ListPreference以按您希望的方式工作,从而创建自己的自定义Preference类。

However, ListPreference is set up to only use a negative ("Cancel") button. 但是, ListPreference设置为仅使用否定(“取消”)按钮。 As the source code says: 正如源代码所说:

    /*
     * The typical interaction for list-based dialogs is to have
     * click-on-an-item dismiss the dialog instead of the user having to
     * press 'Ok'.
     */

The code by techi50 is correct, but does not work for 'cancel button'. techi50的代码是正确的,但不适用于“取消按钮”。 Here are some modification: 以下是一些修改:

protected void onPrepareDialogBuilder(Builder builder) {
    super.onPrepareDialogBuilder(builder);

     prevDialogEntryIndex = getValueIndex();       // add this
     mClickedDialogEntryIndex = getValueIndex();
     builder.setSingleChoiceItems(this.getEntries(), mClickedDialogEntryIndex, new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {
            mClickedDialogEntryIndex = which;

        }
    });

    builder.setPositiveButton("OK", this);
}

public  void onClick (DialogInterface dialog, int which)
{
// when u click Cancel: which = -2; 
// when u click     OK: which = -1; 

    if(which == -2){
      this.setValue(this.getEntryValues()[prevDialogEntryIndex]+"");
    }
    else {
      this.setValue(this.getEntryValues()[mClickedDialogEntryIndex]+"");
    }
}

The solution proposed by Techi50 and ajinkya works ok. Techi50和ajinkya提出的解决方案运行正常。 However, if you also have the OnPreferenceChangeListener it won't fire. 但是,如果您还有OnPreferenceChangeListener,它将不会触发。

    yourListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object o) {

                       //Won't get there

                        preference.setSummary(o.toString());
                        return true;
                    }
                });

To fix this you need to invoke the callChangeListener() function on OK button click, like this: 要解决这个问题,你需要在OK按钮上单击调用callChangeListener()函数,如下所示:

 public  void onClick (DialogInterface dialog, int which) {
        if(which == -2) {
            this.setValue(this.getEntryValues()[mClickedDialogEntryIndexPrev]+"");
        }
        else {
            String value = this.getEntryValues()[mClickedDialogEntryIndex] + "";
            this.setValue(value);
            callChangeListener(value);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM