2012-03-16 46 views
6

我有一個PreferenceActivity,其中包括一個類別,包括呼叫轉移選項。我想要的是一個首選項:是否可以將EditTextPreference與CheckBoxPreference結合使用?

  • 啓用/禁用,如果用戶按右邊的複選框。

    <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> 
    
  • 如果用戶按下文本(或其他任何在優先)

它可能不是任何用處的,但這裏是這個特殊preferencecategory的片斷開闢了EditTextPreference對話框

編輯

我想實現它在這個方法如果可能的話:

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

如果有人仍然不知道 - 這就是我想要的偏好:

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

EDIT3

我已經烤焦現在開了幾個小時,並且提出了一個單詞:'PreferenceGroupAdapter'。但是,我還沒有找到示例或教程來向我展示如何使用它。建議?這是否是正確的道路?

EDIT4

如果這真的是不可能我非常想一個建議的替代方案(人性化)的解決方案,我可以實現的,而不是聯合編輯 - 和複選框偏好。

+0

你有沒有設法解決這個問題?我一直在試圖創建一個像這樣的自定義首選項,無論是使用首選項行上的複選框還是合併到首選項對話框中,但這是我設法在任何地方找到的唯一參考。 – 2013-03-26 13:32:39

+0

@ SeanO'Toole看看我的回答是否有幫助。 – theblang 2013-12-03 16:13:32

回答

2

你可以這樣做。首先,創建一個應該從PreferenceActivity延伸的偏好類。像這樣使用:

// 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; 
    } 
}); 

我希望它有幫助。

+0

我已經有了我的SettingsActivity,顯然它擴展了PreferenceActivity。我不能簡單地在這個類中創建一個遵循你的建議的方法嗎? 另外。你正在使用findPreference()。應該在onCreate()中以編程方式添加這些chechboxpreferences? – CodePrimate 2012-03-16 09:14:54

+0

是的,創建一個名爲'listeners'的方法,並調用'onCreate'方法。它會工作。 – 2012-03-16 09:17:11

+0

我編輯了我的評論:) – CodePrimate 2012-03-16 09:18:02

1

爲您的CheckBoxPreference定義res/values/strings.xml中的密鑰。

給你的CheckBoxPreference的XML屬性android:key="@string/THE_KEY_YOU_DEFINED",使它會自動保存在SharedPreferences狀態。

給你的EditTextPreference XML屬性android:dependency="@string/THE_KEY_YOU_DEFINED

然後根據CheckBoxPreference的狀態啓用/禁用EditTextPreference

0

有點晚了,但我想我已經成功地創建類似創建該類創建與編輯文本和一個複選框佈局的東西:

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; 
    } 
} 

,並把這個變成你的喜好(記住「 。android.example.example.com.androiddemo」是包的名稱):

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

爲了讓你可以嘗試在你的主要活動是這樣的示例中的數據:

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

Disabled Enabled

相關問題