簡體   English   中英

使用listpreference並獲取密鑰有效,但沒有確定按鈕

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

我在我的Android應用程序中使用listpreference並獲取我的鍵值,一切都很好並且運行良好(現在你們幫助了我)但是 - 當我的listpreference菜單彈出時,它們只包含一個取消按鈕。

假設用戶在紅色,藍色和綠色之間進行選擇。 首次彈出列表首選項對話框時,對話框僅顯示取消按鈕。 因此,只要用戶選擇了對話框,對話框就會消失。 我希望這樣當用戶選擇他們的設置時,他們會看到單選按鈕突出顯示然后他們繼續並單擊確定按鈕...但我沒有確定按鈕而無法弄清楚原因。 任何幫助都會很棒......

我已經完成了之前的答案建議並基於Android源代碼實現了我自己的ListPreference。 下面是我添加OK按鈕的實現。

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]+"");
    }

}

然后,您可以在preference.xml中使用該類,如下所示:

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

您可以克隆並重新實現ListPreference以按您希望的方式工作,從而創建自己的自定義Preference類。

但是, ListPreference設置為僅使用否定(“取消”)按鈕。 正如源代碼所說:

    /*
     * 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'.
     */

techi50的代碼是正確的,但不適用於“取消按鈕”。 以下是一些修改:

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]+"");
    }
}

Techi50和ajinkya提出的解決方案運行正常。 但是,如果您還有OnPreferenceChangeListener,它將不會觸發。

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

                       //Won't get there

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

要解決這個問題,你需要在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