1.知识图谱

安卓数据存储之SharedPreferences存储

例子1:模仿登录的记住密码

XML代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:orientation="vertical"  
  9.     tools:context="com.example.cookie.android0623data.LoginActivity">  
  10.   
  11.     <EditText  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/et_login_uname"  
  15.         android:hint="请输入用户名"/>  
  16.     <EditText  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:id="@+id/et_login_upass"  
  20.         android:hint="请输入密码"/>  
  21.     <Button  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="登录"  
  25.         android:onClick="login"/>  
  26.   
  27. </LinearLayout>  


Java代码:

[html] view plain copy
  1. package com.example.cookie.android0623data;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. public class LoginActivity extends AppCompatActivity {  
  12.   
  13.     private EditText et_login_uname;  
  14.     private EditText et_login_upass;  
  15.     private SharedPreferences sp;  
  16.     private SharedPreferences.Editor editor;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_login);  
  22.         et_login_uname = (EditText) findViewById(R.id.et_login_uname);  
  23.         et_login_upass = (EditText) findViewById(R.id.et_login_upass);  
  24.         //只能够自己的文件进行读写  
  25.         sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);  
  26.         //想再sp里面写东西  
  27.         editor = sp.edit();  
  28.   
  29.         //再次进来,获取数据  
  30.         String uname=sp.getString("uname","");  
  31.         String upass=sp.getString("upass","");  
  32.         et_login_uname.setText(uname);  
  33.         et_login_upass.setText(upass);  
  34.     }  
  35.     public void login(View view){  
  36.         //uname=admin  upass=123456  
  37.         String uname=et_login_uname.getText().toString();  
  38.         String upass=et_login_upass.getText().toString();  
  39.         Toast.makeText(this, "跳转页面", Toast.LENGTH_SHORT).show();  
  40.         editor.putString("uname",uname);  
  41.         editor.putString("upass",upass);  
  42.         //细节:  
  43.         editor.commit();  
  44.   
  45.   
  46.     }  
  47. }  



例二,模仿手机的设置界面中的WLAN的开启和关闭

1.首先,在一个项目的res文件下建一个xml包

2.跳转到project项目下,新建一个setting.xml文件

xml代码如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <SwitchPreference  
  4.         android:title="WLAN"  
  5.         android:summaryOn="wifi已开启"  
  6.         android:summaryOff="wifi已关闭"  
  7.         android:key="@+id/sp_set_wifi"  
  8.         ></SwitchPreference>  
  9.   
  10. </PreferenceScreen>  



SettingActivity.java代码如下:

[html] view plain copy
  1. package com.example.cookie.android0623data;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.PersistableBundle;  
  5. import android.preference.PreferenceActivity;  
  6. import android.support.annotation.Nullable;  
  7.   
  8. /**  
  9.  * Created by Administrator on 2017/6/23 0023.  
  10.  */  
  11.   
  12. public class SettinActivity extends PreferenceActivity{  
  13.   
  14.   
  15.   
  16.     @Override  
  17.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         //指定xml文件  
  20.         addPreferencesFromResource(R.xml.setting);  
  21.     }  
  22. }  

相关文章: