Is it possible to combine EditTextPreference with CheckBoxPreference?

I have a PreferenceActivity, among other things, a category, including call forwarding options. I want this preference:

  • Enables / disables if the user clicks the checkbox on the right.
  • Opens the EditTextPreference dialog if the user clicks on the text (or something else in preference)

This is probably not used, but here is a snippet of this particular category of preferences:

<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

I would like to implement it in this method, if possible:

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

If anyone is still interested, this is what I want as a preference:

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

EDIT3

I searched for a couple of hours and came up with one word: "PreferenceGroupAdapter". However, I could not find examples or tutorials showing how to use them. Suggestions? Is this even the right way?

EDIT4

If this is really not possible, I would very much like to propose an alternative (user-friendly) solution that I can implement instead of the combined selection of Edit and Checkbox.

+6
source share
3 answers

Can you do this. First create a preference class that needs to be extended with PreferenceActivity . Use the following:

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

Hope this helps.

+2
source

Define the key in res/values/strings.xml for CheckBoxPreference .

Give the CheckBoxPreference XML attribute android:key="@string/THE_KEY_YOU_DEFINED" so that it automatically SharedPreferences state in SharedPreferences .

Give your EditTextPreference XML attribute android:dependency="@string/THE_KEY_YOU_DEFINED .

EditTextPreference should be enabled / disabled depending on the state of the CheckBoxPreference .

+1
source

A little late, but I think that I managed to create something similar with a dialog box in which a layout with editing text and a flag is created, it should be possible to do the same in a normal layout:

  public class CheckEditTextPreference extends DialogPreference { private static final String KEY_PROPERTY_DISABLED = "key_property_disabled"; private EditText editText; private CheckBox checkBox; private String text; private boolean isDisabled; private SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); public CheckEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected View onCreateDialogView() { return buildUi(); } /** * Build a dialog using an EditText and a CheckBox */ private View buildUi() { FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(25, 0, 0, 0); LinearLayout linearLayout = new LinearLayout(getContext()); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(layoutParams); checkBox = new CheckBox(getContext()); editText = new EditText(getContext()); editText.setLayoutParams(layoutParams); checkBox.setLayoutParams(layoutParams); checkBox.setText("Disabled"); FrameLayout dialogView = new FrameLayout(getContext()); linearLayout.addView(editText); linearLayout.addView(checkBox); dialogView.addView(linearLayout); 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(isDisabled()); editText.setText(getText()); } @Override protected void onDialogClosed(boolean positiveResult) { if (positiveResult) { String text = editText.getText().toString(); boolean isChecked = checkBox.isChecked(); if (callChangeListener(text)) { setText(text); } if (callChangeListener(isChecked)) { isDisabled(isChecked); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getString(index); } @Override protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { setText(restorePersistedValue ? getPersistedString("") : defaultValue.toString()); isDisabled(mySharedPreferences.getBoolean(KEY_PROPERTY_DISABLED, true)); } public void setText(String value) { this.text = value; persistString(this.text); } public String getText() { return this.text; } private void isDisabled(boolean value) { this.isDisabled = value; mySharedPreferences.edit().putBoolean(KEY_PROPERTY_DISABLED, this.isDisabled).apply(); } public boolean isDisabled() { return this.isDisabled; } } 

And put this in your settings screen:

  <your.package.name.CheckEditTextPreference android:key="chkEtPref" android:title="Title"/> 

Disabled Enabled

+1
source

Source: https://habr.com/ru/post/910885/


All Articles