2010-03-25 25 views
8

Tôi đang sử dụng listpreference trong ứng dụng android và nhận các giá trị chính và tất cả đều tốt và hoạt động tốt (bây giờ các bạn đã giúp tôi) NHƯNG - khi các menu listpreference của tôi popup, chúng chỉ chứa nút hủy.Sử dụng listpreference và nhận được các phím hoạt động nhưng không có nút ok

Giả sử người dùng đang chọn giữa màu đỏ, xanh dương và xanh lục. Khi hộp thoại listpreference đầu tiên bật lên, hộp thoại chỉ hiển thị nút hủy. Do đó, hộp thoại sẽ biến mất ngay sau khi người dùng chọn lựa chọn của họ. Tôi muốn nó để khi người dùng chọn cài đặt của họ, họ thấy nút radio được tô sáng và sau đó họ tiếp tục và nhấp vào nút ok ... nhưng tôi không có nút ok và không thể hiểu tại sao. Bất kỳ trợ giúp nào cũng sẽ tuyệt vời ... al

Trả lời

6

Bạn có thể sao chép và thực hiện lại ListPreference để hoạt động theo cách mình muốn, kết quả là làm cho lớp tùy chỉnh của riêng bạn là Preference.

Tuy nhiên, ListPreference được thiết lập để chỉ sử dụng nút phủ định ("Hủy"). Khi mã nguồn cho biết:

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

Tôi đã làm câu trả lời trước đó được đề xuất và triển khai ListPreference của riêng tôi dựa trên mã nguồn Android. Dưới đây là thực hiện của tôi mà thêm nút 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]+""); 
    } 

} 

Sau đó, bạn có thể sử dụng các lớp trong preference.xml của bạn như sau:

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

Để cụ thể hơn: thực hiện DialogInterface.OnClickListener – southerton

4

Mã bởi techi50 là đúng, nhưng không làm việc cho 'nút hủy '. Dưới đây là một số sửa đổi:

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

Giải pháp được đề xuất bởi Techi50 và ajinkya hoạt động tốt. Tuy nhiên, nếu bạn cũng có OnPreferenceChangeListener, nó sẽ không kích hoạt.

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

         //Won't get there 

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

Để khắc phục điều này, bạn cần phải gọi) chức năng (callChangeListener vào OK nút nhấp chuột, như thế này:

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); 
     } 
    } 
Các vấn đề liên quan