电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼

瀏覽:6日期:2022-09-19 09:00:43
1 SharedPreferences 介紹

SharedPreferences是使用鍵值對(duì)的方式來存儲(chǔ)數(shù)據(jù)的

SharedPreferences share = getSharedPreferences('my_file', Context.MODE_PRIVATE);SharedPreferences.Editor editor = share.edit();// 4 保存數(shù)據(jù)到文件editor.putString('account', input_account.getText().toString());editor.putString('password', input_password.getText().toString());editor.putBoolean('pass_remem', pass_remem.isChecked()); // 單選框 選中時(shí)返回為 true

當(dāng)保存一條數(shù)據(jù)的時(shí)候,需要給這條數(shù)據(jù)提供一個(gè)對(duì)應(yīng)的鍵,可以通過這個(gè)把相應(yīng)的值取出來

SharedPreferences sharedPreferences = getSharedPreferences('my_file', Context.MODE_PRIVATE);Boolean pass_remem_ = sharedPreferences.getBoolean('pass_remem', false);

它是一個(gè)輕量級(jí)的存儲(chǔ)類,特別適合用于保存軟件配置參數(shù)。使用SharedPreferences保存數(shù)據(jù),文件存放在/data/data/<package name>/shared_prefs目錄下

1.1 SharedPreferences 四種操作模式 Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容 Context.MODE_APPEND:模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件. MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取. MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入.1.3 使用方法

由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力。但其內(nèi)部有一個(gè)Editor內(nèi)部接口,Editor接口有一系列方法來操作SharedPreference

1.edit( ) 獲得SharedPreferences.Edit對(duì)象 getSharedPreferences('myfile',0).edit( )

2.向?qū)ο笾刑砑訑?shù)據(jù)

putString( ) putInt( ) putBoolean( )

editor.putString(“name”, “張三');editor.putInt(“age”, 21);editor.putBoolean('married',true)

3.commit( ) 提交數(shù)據(jù),完成數(shù)據(jù)存儲(chǔ)操作 editor.commit( );

4.從文件中讀取數(shù)據(jù) 第一個(gè)參數(shù)為KEY 第二個(gè)參數(shù)為訪問失敗時(shí)的默認(rèn)值

getString( ) getInt( ) getBoolean( )

getString ('name', '');getInt (“age', 0);getBoolean (“married', false);2 使用 SharedPreferences 進(jìn)行登錄2.1 前端設(shè)計(jì)

Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.MainActivity'> <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:text='@string/app_name'android:textSize='36sp'android:gravity='center'android:layout_marginTop='100dp'android:layout_marginBottom='10dp' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:text='@string/S_account' android:gravity='center' android:textSize='16sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='2' android:ems='10' android:textSize='16sp' android:paddingLeft='10dp' android:inputType='textPersonName' android:hint='@string/S_input_account'/> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:text='@string/S_password' android:gravity='center' android:textSize='16sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='2' android:ems='10' android:textSize='16sp' android:paddingLeft='10dp' android:inputType='numberPassword' android:hint='@string/S_input_password'/> </LinearLayout> <CheckBoxandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:textSize='16sp'android:layout_gravity='right'android:layout_marginRight='30dp'android:layout_marginBottom='33dp'android:text='@string/S_pass_remem'android:checked='false'/> <Buttonandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:padding='5dp'android:layout_margin='20dp'android:text='@string/S_button_submit'android:textSize='24sp'/></LinearLayout>2.1 Control層

public class MainActivity extends AppCompatActivity { private EditText input_account, input_password; private CheckBox pass_remem; private Button submit_button; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 1 獲取各個(gè)組件的信息, 并存儲(chǔ)到數(shù)據(jù)層input_account = this.findViewById(R.id.input_account);input_password = this.findViewById(R.id.input_password);pass_remem = this.findViewById(R.id.password_remember);submit_button = this.findViewById(R.id.submit);// 2 設(shè)置按鈕的點(diǎn)擊事件submit_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// 3 獲取SharedPreferencesSharedPreferences share = getSharedPreferences('my_file', Context.MODE_PRIVATE);SharedPreferences.Editor editor = share.edit();// 4 保存數(shù)據(jù)到文件editor.putString('account', input_account.getText().toString());editor.putString('password', input_password.getText().toString());editor.putBoolean('pass_remem', pass_remem.isChecked()); // 單選框 選中時(shí)返回為 true// 5 提交數(shù)據(jù), 并進(jìn)行提示editor.commit();Toast.makeText(MainActivity.this, '數(shù)據(jù)寫入成功', Toast.LENGTH_SHORT).show();// App條狀I(lǐng)ntent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent); }});// 6 如果選中,下一次加載數(shù)據(jù)SharedPreferences sharedPreferences = getSharedPreferences('my_file', Context.MODE_PRIVATE);Boolean pass_remem_ = sharedPreferences.getBoolean('pass_remem', false);if (pass_remem_) { String account = sharedPreferences.getString('account', ''); String password = sharedPreferences.getString('password', ''); input_account.setText(account); input_password.setText(password); pass_remem.setChecked(pass_remem_); // 恢復(fù)到原來的狀態(tài)} }}

到此這篇關(guān)于Android 使用 SharedPreferences 保存少量數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Android 保存數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 不锈钢散热器,冷却翅片管散热器厂家-无锡市烨晟化工装备科技有限公司 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂_帽子厂_浙江高普制帽厂 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 福州甲醛检测-福建室内空气检测_环境检测_水质检测-福建中凯检测技术有限公司 | 能量回馈_制动单元_电梯节能_能耗制动_深圳市合兴加能科技有限公司 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 广州工业氧气-工业氩气-工业氮气-二氧化碳-广州市番禺区得力气体经营部 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 | 耐酸碱泵-自吸耐酸碱泵型号「品牌厂家」立式耐酸碱泵价格-昆山国宝过滤机有限公司首页 | 出国劳务公司_正规派遣公司[严海]| 上海赞永| 百方网-百方电气网,电工电气行业专业的B2B电子商务平台 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 地图标注|微信高德百度地图标注|地图标记-做地图[ZuoMap.com] | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 辽宁资质代办_辽宁建筑资质办理_辽宁建筑资质延期升级_辽宁中杭资质代办 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 泉州陶瓷pc砖_园林景观砖厂家_石英砖地铺石价格 _福建暴风石英砖 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 智慧农业|农业物联网|现代农业物联网-托普云农物联网官方网站 | 数控专用机床,专用机床,自动线,组合机床,动力头,自动化加工生产线,江苏海鑫机床有限公司 |