Android基础之SharedPerforences的简单使用示例
白羽 2018-06-27 来源 :网络 阅读 1193 评论 0

摘要:本文将带你了解Android基础之SharedPerforences的简单使用示例,希望本文对大家学Android有所帮助。


 

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出……。下面通过一个小小的案例。

1、最终效果图

 Android基础之SharedPerforences的简单使用示例

大致就是通过SharedPreferences存储类创建一个配置文件(这里是通过按钮去触发的),然后向配置文件中写入配置信息,最后就是读配置中“键”对应的“值”(这里通过按钮触发将读到的值显示出来)。还是比较简单,很容易的。

2、布局文件

activity_main.xml:

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android" 3     xmlns:app="//schemas.android.com/apk/res-auto" 4     xmlns:tools="//schemas.android.com/tools" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="thonlon.example.cn.sharedpreferencesdemo.MainActivity"> 8  9     <Button10         android:id="@+id/btn_create"11         android:layout_width="396dp"12         android:layout_height="46dp"13         android:text="@string/btn_create_sp"14         app:layout_constraintBottom_toBottomOf="parent"15         app:layout_constraintLeft_toLeftOf="parent"16         app:layout_constraintRight_toRightOf="parent"17         app:layout_constraintTop_toTopOf="parent"18         app:layout_constraintVertical_bias="0.0" />19 20     <Button21         android:id="@+id/btn_getInfo"22         android:layout_width="396dp"23         android:layout_height="46dp"24         android:layout_marginLeft="0dp"25         android:text="@string/btn_getInfo_sp"26         app:layout_constraintBottom_toBottomOf="parent"27         app:layout_constraintLeft_toLeftOf="parent"28         app:layout_constraintRight_toRightOf="parent"29         app:layout_constraintTop_toTopOf="parent"30         app:layout_constraintVertical_bias="0.1" />31 32     <TextView33         android:id="@+id/textView1"34         android:layout_width="80dp"35         android:layout_height="20dp"36         android:background="@color/colorPrimary"37         android:text="英文显示:"38         android:textAlignment="center"39         android:textColor="@color/colorWhite"40         app:layout_constraintBottom_toBottomOf="parent"41         app:layout_constraintHorizontal_bias="0.0"42         app:layout_constraintLeft_toLeftOf="parent"43         app:layout_constraintRight_toRightOf="parent"44         app:layout_constraintTop_toTopOf="parent"45         app:layout_constraintVertical_bias="0.465" />46 47     <TextView48         android:id="@+id/textView2"49         android:layout_width="80dp"50         android:layout_height="20dp"51         android:background="@color/colorPrimary"52         android:text="中文显示:"53         android:textAlignment="center"54         android:textColor="@color/colorWhite"55         app:layout_constraintBottom_toBottomOf="parent"56         app:layout_constraintHorizontal_bias="0"57         app:layout_constraintLeft_toLeftOf="parent"58         app:layout_constraintRight_toRightOf="parent"59         app:layout_constraintTop_toTopOf="parent"60         app:layout_constraintVertical_bias="0.205" />61 62     <TextView63         android:id="@+id/tv1"64         android:layout_width="wrap_content"65         android:layout_height="wrap_content"66         app:layout_constraintBottom_toBottomOf="parent"67         app:layout_constraintHorizontal_bias="0.0"68         app:layout_constraintLeft_toLeftOf="parent"69         app:layout_constraintRight_toRightOf="parent"70         app:layout_constraintTop_toTopOf="parent"71         app:layout_constraintVertical_bias="0.244" />72 73     <TextView74         android:id="@+id/tv2"75         android:layout_width="wrap_content"76         android:layout_height="wrap_content"77         app:layout_constraintBottom_toBottomOf="parent"78         app:layout_constraintHorizontal_bias="0.0"79         app:layout_constraintLeft_toLeftOf="parent"80         app:layout_constraintRight_toRightOf="parent"81         app:layout_constraintTop_toTopOf="parent"82         app:layout_constraintVertical_bias="0.53" />83 </android.support.constraint.ConstraintLayout>

 

3、activity文件

MainActivity.java

 

 1 package thonlon.example.cn.sharedpreferencesdemo; 2  3 import android.content.SharedPreferences; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.TextView; 9 import android.widget.Toast;10 11 public class MainActivity extends AppCompatActivity {12 13     private SharedPreferences sharedPreferences;14     private Button btn_create, btn_getInfo;15     private TextView textView1, textView2;16 17     @Override18     protected void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity_main);21         btn_create = (Button) findViewById(R.id.btn_create);22         btn_getInfo = (Button) findViewById(R.id.btn_getInfo);23         textView1 = (TextView) findViewById(R.id.tv1);24         textView2 = (TextView) findViewById(R.id.tv2);25 //        设置配置文件的名称和配置文件的被访问权限26         sharedPreferences = getSharedPreferences("config", MODE_ENABLE_WRITE_AHEAD_LOGGING);27         btn_create.setOnClickListener(new View.OnClickListener() {28             @Override29             public void onClick(View view) {30                 write();31             }32 33             private void write() {34                 //得到配置编辑器35                 SharedPreferences.Editor edit = sharedPreferences.edit();36                 //写入配置信息到配置文件中37                 edit.putString("Chinese", "SharedPreferences是Android平台上一个轻量级的存储类。");38                 edit.putString("English", "SharedPreferences is a lightweight storage class on the Android platform to save some of the common configuration of the application.");39                 //注意以上只是将配置信息写入了内存40                 edit.commit();//提交内存配置信息到本地41                 Toast.makeText(getApplicationContext(), "成功创建文件", Toast.LENGTH_LONG).show();42             }43         });44         btn_getInfo.setOnClickListener(new View.OnClickListener() {45             @Override46             public void onClick(View view) {47                 read();48             }49 50             private void read() {51                 String chinese_info = sharedPreferences.getString("Chinese", "");52                 String english_info = sharedPreferences.getString("English", "");53                 textView1.setText(chinese_info);54                 textView2.setText(english_info);55             }56         });57     }58 }

 

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程