有點晚了,但我想我已經成功地創建類似創建該類創建與編輯文本和一個複選框佈局的東西:
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();
你有沒有設法解決這個問題?我一直在試圖創建一個像這樣的自定義首選項,無論是使用首選項行上的複選框還是合併到首選項對話框中,但這是我設法在任何地方找到的唯一參考。 – 2013-03-26 13:32:39
@ SeanO'Toole看看我的回答是否有幫助。 – theblang 2013-12-03 16:13:32