I have a question because I can't solve the blockage while making the Android app.
I want to change the enable value of another preference according to the true false value of the switch, but in the bind method, findPreference is not available.
If the preference taken over from the onPreferenceChange part is SwitchPreference and the object in SwitchPreference is True, the enable property of another preference should be changed to false, but I don't know the solution.
public class SettingsActivity extends AppCompatPreferenceActivity {
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
//String stringValue = value.toString();
//preference.setSummary(stringValue);
if (preference instanceof SwitchPreference) {
String strValue = value.toString();
if (strValue.equals("true")) {
Log.e("TAG", "onPreferenceChange:: " + strValue );
//
}
}
return true;
}// }// onPreferenceChange(Preference, Object)
};// };// OnPreferenceChangeListener()
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}// }// isXLargeTablet(Context)
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
if (preference instanceof SwitchPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), true));
} } else {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}// }// bindPreferenceSummaryToValue(Preference)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsPreferenceFragment()).commit();
setupActionBar();
}// }// onCreate(Bundle)
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// // Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}// }// setupActionBar()
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}// }// onIsMultiPane()
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class SettingsPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e ("TAG", "SettingsPreferenceFragment:onCreate operational");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_settings);
//Preference startPrefrence = findPreference("select_start_map");
//startPrefrence.setSelectable(false);
//startPrefrence.setEnabled(false);
(SwitchPreference)findPreference("start_map_switch");
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("start_map_switch"));
}// }// onCreate(Bundle)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Log.d ("TAG", "SettingsActivity -> Settings -> on OptionsItemSelected <- Clicked"));
startActivity(new Intent(getActivity(), MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Log.e("TAG", "onPreferenceTreeClick::" + preference.getKey());
// Context context = getContext(); // // context.startActivity(SettingsActivity.class); // // startActivity(new Intent(Intent.ac, SettingsActivity.class));
if (preference.getKey().equals("select_start_map")) {
Log.e ("TAG", "onPreferenceTreeClick: preference.getKey().equals(select_start_map) is the same");
}
// return super.onPreferenceTreeClick(preferenceScreen, preference); return false; } } }
android preference
You can invoke findPreference()
through getPreferenceManager()
.
private final preference.OnPreferenceChangeListener=newPreference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.getPreferenceManager().findPreference("your_preference")
return false;
}
};
© 2024 OneMinuteCode. All rights reserved.