APIDEMO PREFERENCE 学习 ACTIVTIY

Prefercence 观察来看设计思想应该是简化设置的UI的界面布局所用。

主题是 PreFerenceActivity

1:xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/inline_preferences"> <CheckBoxPreference android:key="checkbox_preference" android:title="@string/title_toggle_preference" android:summary="@string/summary_toggle_preference" /> </PreferenceCategory> <PreferenceCategory android:title="@string/dialog_based_preferences"> <EditTextPreference android:key="edittext_preference" android:title="@string/title_edittext_preference" android:summary="@string/summary_edittext_preference" android:dialogTitle="@string/dialog_title_edittext_preference" /> <ListPreference android:key="list_preference" android:title="@string/title_list_preference" android:summary="@string/summary_list_preference" android:entries="@array/entries_list_preference" android:entryValues="@array/entryvalues_list_preference" android:dialogTitle="@string/dialog_title_list_preference" /> </PreferenceCategory> <PreferenceCategory android:title="@string/launch_preferences"> <!-- This PreferenceScreen tag serves as a screen break (similar to page break in word processing). Like for other preference types, we assign a key here so it is able to save and restore its instance state. --> <PreferenceScreen android:key="screen_preference" android:title="@string/title_screen_preference" android:summary="@string/summary_screen_preference"> <!-- You can place more preferences here that will be shown on the next screen. --> <CheckBoxPreference android:key="next_screen_checkbox_preference" android:title="@string/title_next_screen_toggle_preference" android:summary="@string/summary_next_screen_toggle_preference" /> </PreferenceScreen> <PreferenceScreen android:title="@string/title_intent_preference" android:summary="@string/summary_intent_preference"> <intent android:action="android.intent.action.VIEW" android:data="http://www.android.com" /> </PreferenceScreen> </PreferenceCategory> <PreferenceCategory android:title="@string/preference_attributes"> <CheckBoxPreference android:key="parent_checkbox_preference" android:title="@string/title_parent_preference" android:summary="@string/summary_parent_preference" /> <!-- The visual style of a child is defined by this styled theme attribute. --> <CheckBoxPreference android:key="child_checkbox_preference" android:dependency="parent_checkbox_preference" android:layout="?android:attr/preferenceLayoutChild" android:title="@string/title_child_preference" android:summary="@string/summary_child_preference" /> </PreferenceCategory> </PreferenceScreen>

Code实现:

private PreferenceScreen createPreferenceHierarchy() { // Root PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); // Inline preferences PreferenceCategory inlinePrefCat = new PreferenceCategory(this); inlinePrefCat.setTitle(R.string.inline_preferences); root.addPreference(inlinePrefCat); // Toggle preference CheckBoxPreference togglePref = new CheckBoxPreference(this); togglePref.setKey("toggle_preference"); togglePref.setTitle(R.string.title_toggle_preference); togglePref.setSummary(R.string.summary_toggle_preference); inlinePrefCat.addPreference(togglePref); // Dialog based preferences PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this); dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences); root.addPreference(dialogBasedPrefCat); // Edit text preference EditTextPreference editTextPref = new EditTextPreference(this); editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference); editTextPref.setKey("edittext_preference"); editTextPref.setTitle(R.string.title_edittext_preference); editTextPref.setSummary(R.string.summary_edittext_preference); dialogBasedPrefCat.addPreference(editTextPref); // List preference ListPreference listPref = new ListPreference(this); listPref.setEntries(R.array.entries_list_preference); listPref.setEntryValues(R.array.entryvalues_list_preference); listPref.setDialogTitle(R.string.dialog_title_list_preference); listPref.setKey("list_preference"); listPref.setTitle(R.string.title_list_preference); listPref.setSummary(R.string.summary_list_preference); dialogBasedPrefCat.addPreference(listPref); // Launch preferences PreferenceCategory launchPrefCat = new PreferenceCategory(this); launchPrefCat.setTitle(R.string.launch_preferences); root.addPreference(launchPrefCat); /* * The Preferences screenPref serves as a screen break (similar to page * break in word processing). Like for other preference types, we assign * a key here so that it is able to save and restore its instance state. */ // Screen preference PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this); screenPref.setKey("screen_preference"); screenPref.setTitle(R.string.title_screen_preference); screenPref.setSummary(R.string.summary_screen_preference); launchPrefCat.addPreference(screenPref); /* * You can add more preferences to screenPref that will be shown on the * next screen. */ // Example of next screen toggle preference CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this); nextScreenCheckBoxPref.setKey("next_screen_toggle_preference"); nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference); nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference); screenPref.addPreference(nextScreenCheckBoxPref); // Intent preference PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this); intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW) .setData(Uri.parse("http://www.android.com"))); intentPref.setTitle(R.string.title_intent_preference); intentPref.setSummary(R.string.summary_intent_preference); launchPrefCat.addPreference(intentPref); // Preference attributes PreferenceCategory prefAttrsCat = new PreferenceCategory(this); prefAttrsCat.setTitle(R.string.preference_attributes); root.addPreference(prefAttrsCat); // Visual parent toggle preference CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this); parentCheckBoxPref.setTitle(R.string.title_parent_preference); parentCheckBoxPref.setSummary(R.string.summary_parent_preference); parentCheckBoxPref.setKey("test"); prefAttrsCat.addPreference(parentCheckBoxPref); // Visual child toggle preference // See res/values/attrs.xml for the <declare-styleable> that defines // TogglePrefAttrs. //代码有个问题就是父子的依赖处理没有实现,如何实现本人 //用setDependent()没有测试出来,xml的依赖是对的。 TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs); CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this); childCheckBoxPref.setTitle(R.string.title_child_preference); childCheckBoxPref.setSummary(R.string.summary_child_preference); childCheckBoxPref.setDisableDependentsState(true); childCheckBoxPref.setLayoutResource( a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild, 0)); prefAttrsCat.addPreference(childCheckBoxPref); a.recycle(); return root;

亦可以自定义PreFerecnce控件 MyPreference

public class MyPreference extends Preference { private int mClickCounter; // This is the constructor called by the inflater public MyPreference(Context context, AttributeSet attrs) { super(context, attrs); setWidgetLayoutResource(R.layout.preference_widget_mypreference); } @Override protected void onBindView(View view) { super.onBindView(view); // Set our custom views inside the layout final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget); if (myTextView != null) { myTextView.setText(String.valueOf(mClickCounter)); } } @Override protected void onClick() { int newValue = mClickCounter + 1; // Give the client a chance to ignore this change if they deem it // invalid if (!callChangeListener(newValue)) { // They don't want the value to be set return; } // Increment counter mClickCounter = newValue; // Save to persistent storage (this method will make sure this // preference should be persistent, along with other useful checks) persistInt(mClickCounter); // Data has changed, notify so UI can be refreshed! notifyChanged(); } @Override protected Object onGetDefaultValue(TypedArray a, int index) { // This preference type's value type is Integer, so we read the default // value from the attributes as an Integer. return a.getInteger(index, 0); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { if (restoreValue) { // Restore state mClickCounter = getPersistedInt(mClickCounter); } else { // Set state int value = (Integer) defaultValue; mClickCounter = value; persistInt(value); } } @Override protected Parcelable onSaveInstanceState() { /* * Suppose a client uses this preference type without persisting. We * must save the instance state so it is able to, for example, survive * orientation changes. */ final Parcelable superState = super.onSaveInstanceState(); if (isPersistent()) { // No need to save instance state since it's persistent return superState; } // Save the instance state final SavedState myState = new SavedState(superState); myState.clickCounter = mClickCounter; return myState; } @Override protected void onRestoreInstanceState(Parcelable state) { if (!state.getClass().equals(SavedState.class)) { // Didn't save state for us in onSaveInstanceState super.onRestoreInstanceState(state); return; } // Restore the instance state SavedState myState = (SavedState) state; super.onRestoreInstanceState(myState.getSuperState()); mClickCounter = myState.clickCounter; notifyChanged(); } /** * SavedState, a subclass of {@link BaseSavedState}, will store the state * of MyPreference, a subclass of Preference. * <p> * It is important to always call through to super methods. */ private static class SavedState extends BaseSavedState { int clickCounter; public SavedState(Parcel source) { super(source); // Restore the click counter clickCounter = source.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); // Save the click counter dest.writeInt(clickCounter); } public SavedState(Parcelable superState) { super(superState); } public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } }

<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Custom preference type using a text view. --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mypreference_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="6sp" android:focusable="false" android:clickable="false" />

使用:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- My custom preference type. This just replaces the actual widget portion of the preference, if the whole preference wanted to be replaced we would use the layout attribute instead of the widgetLayout attribute. --> <com.example.android.apis.app.MyPreference android:key="my_preference" android:title="@string/title_my_preference" android:summary="@string/summary_my_preference" android:defaultValue="100" /> <CheckBoxPreference android:key="advanced_checkbox_preference" android:title="@string/title_advanced_toggle_preference" android:summaryOn="@string/summary_on_advanced_toggle_preference" android:summaryOff="@string/summary_off_advanced_toggle_preference" /> </PreferenceScreen>

public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { public static final String KEY_MY_PREFERENCE = "my_preference"; public static final String KEY_ADVANCED_CHECKBOX_PREFERENCE = "advanced_checkbox_preference"; private CheckBoxPreference mCheckBoxPreference; private Handler mHandler = new Handler(); /** * This is a simple example of controlling a preference from code. */ private Runnable mForceCheckBoxRunnable = new Runnable() { public void run() { if (mCheckBoxPreference != null) { mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked()); } // Force toggle again in a second mHandler.postDelayed(this, 1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the XML preferences file addPreferencesFromResource(R.xml.advanced_preferences); // Get a reference to the checkbox preference mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference( KEY_ADVANCED_CHECKBOX_PREFERENCE); } @Override protected void onResume() { super.onResume(); // Start the force toggle mForceCheckBoxRunnable.run(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override protected void onPause() { super.onPause(); // Unregister the listener whenever a key changes getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); mHandler.removeCallbacks(mForceCheckBoxRunnable); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // Let's do something when my counter preference value changes if (key.equals(KEY_MY_PREFERENCE)) { Toast.makeText(this, "Thanks! You increased my count to " + sharedPreferences.getInt(key, 0), Toast.LENGTH_SHORT).show(); } } }

可以在Activity 主UI 线程中 使用preference xml 的值

1:先在主UI中定义

/*
* If this were my app's main activity, I would load the default values
* so they're set even if the user does not go into the preferences
* screen. Another good place to call this method would be from a
* subclass of Application, so your default values would be loaded
* regardless of entry into your application (for example, a service or
* activity).
*/
PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

2: 使用

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0);

**项目名称:** 基于Vue.js与Spring Cloud架构的博客系统设计与开发——微服务分布式应用实践 **项目概述:** 本项目为计算机科学与技术专业本科毕业设计成果,旨在设计并实现一个采用前后端分离架构的现代化博客平台。系统前端基于Vue.js框架构建,提供响应式用户界面;后端采用Spring Cloud微服务架构,通过服务拆分、注册发现、配置中心及网关路由等技术,构建高可用、易扩展的分布式应用体系。项目重点探讨微服务模式下的系统设计、服务治理、数据一致性及部署运维等关键问题,体现了分布式系统在Web应用中的实践价值。 **技术架构:** 1. **前端技术栈:** Vue.js 2.x、Vue Router、Vuex、Element UI、Axios 2. **后端技术栈:** Spring Boot 2.x、Spring Cloud (Eureka/Nacos、Feign/OpenFeign、Ribbon、Hystrix、Zuul/Gateway、Config) 3. **数据存储:** MySQL 8.0(主数据存储)、Redis(缓存与会话管理) 4. **服务通信:** RESTful API、消息队列(可选RabbitMQ/Kafka) 5. **部署与运维:** Docker容器化、Jenkins持续集成、Nginx负载均衡 **核心功能模块:** - 用户管理:注册登录、权限控制、个人中心 - 文章管理:富文本编辑、分类标签、发布审核、评论互动 - 内容展示:首页推荐、分类检索、全文搜索、热门排行 - 系统管理:后台仪表盘、用户与内容监控、日志审计 - 微服务治理:服务健康检测、动态配置更新、熔断降级策略 **设计特点:** 1. **架构解耦:** 前后端完全分离,通过API网关统一接入,支持独立开发与部署。 2. **服务拆分:** 按业务域划分为用户服务、文章服务、评论服务、文件服务等独立微服务。 3. **高可用设计:** 采用服务注册发现机制,配合负载均衡与熔断器,提升系统容错能力。 4. **可扩展性:** 模块化设计支持横向扩展,配置中心实现运行时动态调整。 **项目成果:** 完成了一个具备完整博客功能、具备微服务典型特征的分布式系统原型,通过容器化部署验证了多服务协同运行的可行性,为云原生应用开发提供了实践参考。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值