2010-03-25 15 views
8

alma ve benim anahtar değerlerini alma ve tüm iyi ve (şimdi siz bana yardım ettiğim) iyi işler AMA - benim listpreference menüler pop-up, sadece bir iptal düğmesi içerir. listpreference kullanma ve benim android app listpreference kullanıyorum anahtar eserler ancak hiçbir Tamam düğmesine

kullanıcı kırmızı, mavi ve yeşil arasında bir seçim yaptığını varsayalım. Liste önceliği iletişim kutusu ilk açıldığında, diyalog sadece bir iptal düğmesi gösterir. Bu nedenle, kullanıcı kendi seçimini seçtiğinde iletişim kutusu kaybolur. kullanıcı kendi ayarını seçtiğinde, onlar radyo düğmesi vurgulanır görmek ve daha sonra devam edin ve tamam butonuna tıklayın, böylece bunu istiyorum ... ama bir ok düğmesi yok ve neden bilemiyorum. Herhangi bir yardım harika olurdu ... al

cevap

6

İstediğiniz şekilde çalışmak için ListPreference klonlayıp yeniden kullanabilirsiniz ve sonuç olarak kendi özel Preference sınıfınızı oluşturabilirsiniz.

Ancak ListPreference sadece ("İptal") düğmesini bir negatif kullanmak üzere ayarlandı. Kaynak kodu söylediği gibi: Ben önceki cevabı önerdi ve Android kaynak koduna dayalı kendi ListPreference hayata neyi yapmış

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

. Aşağıda, Tamam düğmesine ekleyen uygulamam var.

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

} 

Sonra aşağıdaki gibi sizin preference.xml sınıfı kullanabilirsiniz:

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

daha spesifik olmak gerekirse:

yourListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object o) { //Won't get there preference.setSummary(o.toString()); return true; } }); 

böyle, tamam düğmesi tıklandığında callChangeListener() fonksiyonunu çağırmak gerekir Bunu düzeltmek için DialogInterface.OnClickListener uygular – southerton

4

techi50 kod doğru fakat iptal düğmesi' için çalışmıyor '. Techi50 ve Ajinkya önerdiği çözüm Tamam çalışır

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

: İşte bazı değişiklik bulunmaktadır. Ancak, aynı zamanda varsa OnPreferenceChangeListener o patlamaz.

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); 
     } 
    } 
İlgili konular