【发布时间】:2015-03-01 17:50:17
【问题描述】:
我正在使用带有两个片段的 PreferenceActivity,每个片段都包含一个 PreferenceScreen。
我想做的是在我拥有的自定义首选项上创建一个事件侦听器(此自定义首选项的一行是带有 SwitchView 的 TextView)。我很好地处理了 Switch 首选项,它可以根据需要保存它,但现在我想做的是在 SwitchView 之间的 TextView 上添加一个事件,以在屏幕的另一部分(第二个片段)显示我想要的内容。
让我向您展示我当前的代码。 这是我的 CustomPreference(它只是一个 TextView 和一个 Switch)
public class ItemPreference extends Preference{
private static TextView text; // this is the text at the left of the switch, it's where i want to handle the event
// to show an other preferencescreen in the fragment between
private Switch switcher; // the key of the preference is for the SWITCH !!
private boolean checked;
private Context context;
public TextView getCustomText(){
return text;
}
public ItemPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setLayoutResource(R.layout.row_setting); // the layout resource of my custom preference
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
text = (TextView)view.findViewById(R.id.buttonRowSetting);
// to display the text in the TextView between the Switch, I use the key of the Switch
if (this.getKey().equals("pref_key_classification"))
text.setText(R.string.title_activity_classification);
// the switchview and it's preference saving
switcher = (Switch)view.findViewById(R.id.switchRowSetting);
Boolean value = this.getPersistedBoolean(false);
if (value){
switcher.setChecked(true);
} else {
switcher.setChecked(false);
}
switcher.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setSwitchChecked(isChecked);
}
});
}
public void setSwitchChecked(boolean value) {
if (checked != value) {
checked = value;
persistBoolean(value);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getBoolean(index, false);
}
@Override
protected void onClick() {
super.onClick();
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
setSwitchChecked(restorePersistedValue ? getPersistedBoolean(checked) : (Boolean) defaultValue);
}
@Override
protected Parcelable onSaveInstanceState() {
return super.onSaveInstanceState();
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
}
这是 SettingsFragment,位于屏幕左侧,我要在其中处理事件以在屏幕的另一部分显示我想要的内容。
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings); // the left preferencefragment
Preference stat = (Preference) findPreference(getString(R.string.pref_key_statistic));
stat.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if(getActivity() instanceof OnSettingPageListener){
// if i click on this preference, this loads this preferencescreen on the other fragment, works well.
((OnSettingPageListener)getActivity()).onSettingPageChange(R.xml.settings_stat);
}
return true;
}
});
// this is the custompreference, i would like to handle here a listener on the TextView to display a specific preferencescreen on the other fragment
ItemPreference classif = (ItemPreference) findPreference(getString(R.string.pref_key_classification));
// i tried this, and i also tried to make my own Listener also, but doesn't works
classif.getCustomText().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() instanceof OnSettingPageListener){
// the content i want to load on the other fragment
((OnSettingPageListener)getActivity()).onSettingPageChange(R.xml.settings_classification);
}
}
});
}
// [...]
}
所以我希望你明白我的问题是什么,这只是听众的问题。有人有解决方案吗?
【问题讨论】:
-
不,没明白。你到底想做什么?
标签: android android-fragments listener preferences