2012-03-16 27 views
6

Tôi có một PreferenceActivity với, trong số những thứ khác, một thể loại bao gồm các tùy chọn chuyển tiếp cuộc gọi. Những gì tôi muốn là sở thích:Có thể kết hợp EditTextPreference với một CheckBoxPreference không?

  • Bật/tắt nếu người dùng nhấn vào hộp kiểm ở bên phải.
  • Mở lên hộp thoại EditTextPreference nếu người dùng nhấn các văn bản (hoặc bất cứ điều gì khác trong sở thích)

Nó có lẽ không phải của bất kỳ sử dụng nhưng đây là một đoạn preferencecategory đặc biệt này:

<PreferenceCategory 
     android:title="@string/category_callforward"> 

    <EditTextPreference 
      android:key="call_forward_always" 
      android:title="@string/call_forward_always" 
      android:summary="@string/call_forward_forwardto" /> 
    </PreferenceCategory> 

EDIT

tôi muốn thực hiện nó trong phương pháp này nếu có thể:

// Locates the correct data from saved preferences and sets input type to numerics only 
private void setCallForwardType() 
{ 
    ep1 = (EditTextPreference) findPreference("call_forward_always"); 

    EditText et = (EditText) ep1.getEditText(); 
    et.setKeyListener(DigitsKeyListener.getInstance()); 

} 

EDIT2

Nếu ai vẫn còn tự hỏi - đây là những gì tôi muốn là một Sở thích:

http://i.imgur.com/GHOeK.png

EDIT3

Tôi đã sear ched xung quanh cho một vài giờ bây giờ và đã đưa ra một từ duy nhất: 'PreferenceGroupAdapter'. Tuy nhiên, tôi đã không thể tìm thấy các ví dụ hoặc hướng dẫn chỉ cho tôi cách sử dụng nó. Gợi ý? Đây có phải là con đường chính xác để đi không?

EDIT4

Nếu điều này thực sự không phải là có thể tôi sẽ rất giống một gợi ý để thay thế giải pháp (sử dụng) mà tôi có thể thực hiện thay cho Edit- và Checkbox ưu tiên cộng lại.

+0

Bạn đã bao giờ giải quyết được vấn đề này chưa? Tôi đã cố gắng tạo tùy chọn tùy chỉnh như thế này, hoặc với hộp kiểm trên hàng tùy chọn hoặc được tích hợp vào hộp thoại tùy chọn, nhưng đây là tham chiếu duy nhất tôi đã quản lý tìm thấy ở bất kỳ đâu. –

+0

@ SeanO'Toole Xem câu trả lời của tôi có giúp ích gì không. – theblang

Trả lời

2

Bạn có thể thực hiện việc này. Đầu tiên, tạo một lớp cho các tùy chọn cần được mở rộng từ PreferenceActivity. Sử dụng như sau:

// editbox ise your EditTextPreference, so set it. 
checkbox = (CheckBoxPreference) findPreference("checkbox_preference"); 

checkbox.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
    public boolean onPreferenceChange(Preference preference, Object newValue) { 
     if(newValue.toString().equals("false")) { 
      PrefActivity.this.editbox.setEnabled(false); 
     } else if(newValue.toString().equals("true")) { 
      PrefActivity.this.editbox.setEnabled(true); 
     } 
     return true; 
    } 
}); 

Tôi hy vọng điều đó sẽ hữu ích.

+0

Tôi đã có SettingsActivity của tôi, rõ ràng, mở rộng PreferenceActivity. Tôi không thể chỉ đơn giản là tạo ra một phương pháp trong lớp này mà theo đề nghị của bạn? Ngoài ra. bạn đang sử dụng findPreference(). Các chechboxpreferences này có nên được thêm vào chương trình trong onCreate() không? – CodePrimate

+0

Có, tạo một phương thức có tên là 'listeners' và gọi phương thức' onCreate'. Nó sẽ hoạt động. –

+0

Tôi đã chỉnh sửa bình luận của tôi :) – CodePrimate

1

Xác định khóa trong res/values/strings.xml cho CheckBoxPreference của bạn.

Cung cấp cho CheckBoxPreference thuộc tính XML android:key="@string/THE_KEY_YOU_DEFINED" để thuộc tính này sẽ tự động lưu trạng thái trong SharedPreferences.

Tặng EditTextPreference thuộc tính XML android:dependency="@string/THE_KEY_YOU_DEFINED của bạn.

Sau đó, EditTextPreference sẽ bật/tắt tùy thuộc vào trạng thái của CheckBoxPreference.

0

Một hơi muộn nhưng tôi nghĩ rằng tôi đã cố gắng tạo ra một cái gì đó tương tự như việc tạo ra lớp này tạo ra một bố trí với một văn bản chỉnh sửa và một hộp kiểm:

public class CheckEditTextPreference extends DialogPreference { 
    protected static final String chkKey="ChkKey"; 
    private EditText editText; 
    private CheckBox checkBox; 
    private String etValue; 
    private boolean chkValue; 
    SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); 

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

    } 

    public CheckEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     layoutParams.setMargins(25, 0, 0, 0); 
     LinearLayout llChkEtPreference=new LinearLayout(getContext()); 
     llChkEtPreference.setOrientation(LinearLayout.VERTICAL); 
     llChkEtPreference.setLayoutParams(layoutParams); 
     checkBox=new CheckBox(getContext()); 
     editText = new EditText(getContext()); 
     editText.setLayoutParams(layoutParams); 
     checkBox.setLayoutParams(layoutParams); 
     checkBox.setText("Disabled"); 
     FrameLayout dialogView = new FrameLayout(getContext()); 
     llChkEtPreference.addView(editText); 
     llChkEtPreference.addView(checkBox); 
     dialogView.addView(llChkEtPreference); 
     checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       editText.setEnabled(!isChecked); 
      } 
     }); 
     return dialogView; 
    } 

    @Override 
    protected void onBindDialogView(View view) { 
     super.onBindDialogView(view); 
     checkBox.setChecked(getValueChk()); 
     editText.setText(getValueEt()); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     if (positiveResult) { 
      String newValue = editText.getText().toString(); 
      boolean newValueChk=checkBox.isChecked(); 
      if (callChangeListener(newValue)) { 
       setValueEt(newValue); 
      } 
      if(callChangeListener(newValueChk)) 
      { 
       setValueChk(newValueChk); 
      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return a.getString(index); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { 
     setValueEt(restorePersistedValue ? getPersistedString("") : defaultValue.toString()); 
     setValueChk(mySharedPreferences.getBoolean(chkKey, true)); 
    } 

    public void setValueEt(String value) { 
     this.etValue = value; 
     persistString(this.etValue); 
    } 

    public String getValueEt() { 
     return this.etValue; 
    } 
    public void setValueChk(boolean value) { 
     this.chkValue = value; 
     mySharedPreferences.edit().putBoolean(chkKey, this.chkValue).apply(); 
    } 

    public boolean getValueChk() { 
     return this.chkValue; 
    } 
} 

Và đặt này vào sở thích của bạn (hãy nhớ rằng " . android.example.example.com.androiddemo" là tên của gói):

<android.example.example.com.androiddemo.CheckEditTextPreference 
     android:key="chkEtPref" 
     android:title="Title"/> 

Để có được dữ liệu bạn có thể thử một cái gì đó giống như ví dụ này trong hoạt động chính của bạn:

if(!mySharedPreferences.getBoolean(CheckEditTextPreference.chkKey, true)) 
    Toast.makeText(this, mySharedPreferences.getString("chkEtPref", "Testing"),Toast.LENGTH_LONG).show(); 

Disabled Enabled

Các vấn đề liên quan