Android 是否可以将EditTextPreference与CheckBoxPreference结合使用?
我有一个偏好活动,其中包括一个类别,包括远期期权。我想要的是一个偏好:Android 是否可以将EditTextPreference与CheckBoxPreference结合使用?,android,preferenceactivity,Android,Preferenceactivity,我有一个偏好活动,其中包括一个类别,包括远期期权。我想要的是一个偏好: 如果用户按下右侧的复选框,则启用/禁用 如果用户按下文本(或首选项中的任何其他内容),则打开EditTextPreference对话框 它可能没有任何用处,但下面是这个特定首选项类别的一个片段: <PreferenceCategory android:title="@string/category_callforward"> <EditTextPreference
- 如果用户按下右侧的复选框,则启用/禁用
- 如果用户按下文本(或首选项中的任何其他内容),则打开EditTextPreference对话框
<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>
EDIT2
如果有人还想知道-这是我想要的优先选择:
EDIT3
我已经搜索了几个小时,找到了一个词:“PreferenceGroupAdapter”。然而,我还没有找到演示如何使用它的示例或教程。建议?这是正确的道路吗
EDIT4
如果这真的不可能,我非常希望有一个替代的(用户友好的)解决方案,我可以实现,而不是组合编辑和复选框首选项的建议。你可以这样做。首先,为首选项创建一个类,该类应该从
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;
}
});
我希望它能有所帮助。在
res/values/strings.xml
中为您的复选框首选项定义一个键
为您的CheckBoxPreference
指定XML属性android:key=“@string/the_key\u YOU_定义的”
,以便它将状态自动保存在SharedReferences
中
为您的EditTextPreference
提供XML属性android:dependency=“@string/the_KEY\u YOU_定义的”
然后,EditTextPreference
应该根据CheckBoxPreference
的状态启用/禁用,虽然有点晚了,但我想我已经成功创建了类似的对话框,该对话框创建了一个带有编辑文本和复选框的布局,应该可以在正常布局中执行相同的操作:
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;
}
}
并将其放入您的首选项屏幕:
<your.package.name.CheckEditTextPreference
android:key="chkEtPref"
android:title="Title"/>
我已经有了我的SettingsActivity,它显然扩展了PreferenceActivity。我能不能在这个类中按照你的建议创建一个方法?另外,你正在使用findPreference()。这些chechboxpreferences是否应该以编程方式添加到onCreate()中?是的,创建一个名为listeners
的方法,并调用onCreate
方法。它会起作用。不一定。您可以在onCreate
或new method中定义。也不要忘了EditTextPreference
。请参阅我的编辑。那么,我应该在这里创建一个checkboxpreferences数组,然后在这里实现您的建议吗?(setCallForwardType()在onCreate中被调用)您曾经解决过这个问题吗?我一直在尝试创建这样一个自定义首选项,可以使用首选项行上的复选框,也可以合并到首选项对话框中,但这是我在任何地方都能找到的唯一引用。@SeanO'Toole看看我的答案是否有用。
<your.package.name.CheckEditTextPreference
android:key="chkEtPref"
android:title="Title"/>