可以使用PreferenceScreen以及PreferenceFragment类来实现从设置中更新/删除列表的功能。可以按照以下步骤进行操作:
例如:
例如:
public class SettingsActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new MainSettingsFragment()).commit(); }
public static class MainSettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_screen);
}
} }
例如:
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);
Preference updateListPreference = findPreference("update list"); updateListPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { // update list logic return true; } });
Preference deleteListPreference = findPreference("delete list"); deleteListPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { // delete list logic return true; } }); }
之后,就可以在偏好设置中看到“update list”和“delete list”选项,并通过相应的OnClick事件来处理它们的逻辑。