9

Androidアプリでlistpreferenceを使用していて、キー値を取得していますが、すべてうまく機能しています(皆さんが私を助けてくれたので)しかし、listpreferenceメニューがポップアップすると、キャンセルボタンしかありません。

ユーザーが赤、青、緑のいずれかを選択しているとします。listpreference ダイアログが最初にポップアップするとき、ダイアログにはキャンセル ボタンのみが表示されます。そのため、ユーザーが選択するとすぐにダイアログが消えます。ユーザーが設定を選択すると、ラジオボタンが強調表示され、次に進んで[OK]ボタンをクリックできるようにしたいと思います...しかし、[OK]ボタンがなく、理由がわかりません。どんな助けでも素晴らしいでしょう...アル

4

4 に答える 4

6

前の回答が示唆したことを実行し、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" />
于 2014-04-02T22:31:12.987 に答える
6

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'.
     */
于 2010-03-25T23:11:39.513 に答える
4

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]+"");
    }
}
于 2015-03-15T07:03:37.090 に答える
1

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);
        }
    }
于 2015-09-17T09:24:21.643 に答える