Android 开发笔记___存储方式__共享参数__sharedprefences

胸怀万里世界, 放眼无限未来。这篇文章主要讲述Android 开发笔记___存储方式__共享参数__sharedprefences相关的知识,希望能为你提供帮助。
 
android 的数据存储方式有四种,这次是【共享参数__sharedprefences】
听起来挺别扭的,平时看到的app里面,当用户删除了一些软件以后下次安装,发现原来的设置还在,这种情况就是把一些用户的设置保存在手机里面的一个存储区域,

  • 格式是XML
  • key__Value
  • 不方便保存关系比较复杂的数据
write
1 package com.example.alimjan.hello_world; 2 3 /** 4* Created by alimjan on 7/4/2017. 5*/ 6 7import android.content.Context; 8import android.content.Intent; 9import android.content.SharedPreferences; 10import android.os.Bundle; 11import android.support.v7.app.AppCompatActivity; 12import android.view.View; 13import android.view.View.OnClickListener; 14import android.widget.AdapterView; 15import android.widget.ArrayAdapter; 16import android.widget.EditText; 17import android.widget.Spinner; 18import android.widget.Toast; 19import android.widget.AdapterView.OnItemSelectedListener; 20 21import com.example.alimjan.hello_world.Utils.DateUtil; 22 23 public class class_4_1_1 extends AppCompatActivity implements OnClickListener { 24 25private SharedPreferences mShared; 26private EditText et_name; 27private EditText et_age; 28private EditText et_height; 29private EditText et_weight; 30private boolean bMarried = false; 31 32@Override 33protected void onCreate(Bundle savedInstanceState) { 34super.onCreate(savedInstanceState); 35setContentView(R.layout.code_4_1_1); 36et_name = (EditText) findViewById(R.id.et_name); 37et_age = (EditText) findViewById(R.id.et_age); 38et_height = (EditText) findViewById(R.id.et_height); 39et_weight = (EditText) findViewById(R.id.et_weight); 40findViewById(R.id.btn_save).setOnClickListener(this); 41 42ArrayAdapter< String> typeAdapter = new ArrayAdapter< String> (this, 43R.layout.item_select, typeArray); 44typeAdapter.setDropDownViewResource(R.layout.item_dropdown); 45Spinner sp_married = (Spinner) findViewById(R.id.sp_married); 46sp_married.setPrompt("请选择婚姻状况"); 47sp_married.setAdapter(typeAdapter); 48sp_married.setSelection(0); 49sp_married.setOnItemSelectedListener(new TypeSelectedListener()); 50 51mShared = getSharedPreferences("share", MODE_PRIVATE); 52} 53 54private String[] typeArray = {"未婚", "已婚"}; 55class TypeSelectedListener implements OnItemSelectedListener { 56public void onItemSelected(AdapterView< ?> arg0, View arg1, int arg2, long arg3) { 57bMarried = (arg2==0)?false:true; 58} 59 60public void onNothingSelected(AdapterView< ?> arg0) { 61} 62} 63 64@Override 65public void onClick(View v) { 66if (v.getId() == R.id.btn_save) { 67String name = et_name.getText().toString(); 68String age = et_age.getText().toString(); 69String height = et_height.getText().toString(); 70String weight = et_weight.getText().toString(); 71if (name==null || name.length()< =0) { 72showToast("请先填写姓名"); 73return; 74} 75if (age==null || age.length()< =0) { 76showToast("请先填写年龄"); 77return; 78} 79if (height==null || height.length()< =0) { 80showToast("请先填写身高"); 81return; 82} 83if (weight==null || weight.length()< =0) { 84showToast("请先填写体重"); 85return; 86} 87 88SharedPreferences.Editor editor = mShared.edit(); 89editor.putString("name", name); 90editor.putInt("age", Integer.parseInt(age)); 91editor.putLong("height", Long.parseLong(height)); 92editor.putFloat("weight", Float.parseFloat(weight)); 93editor.putBoolean("married", bMarried); 94editor.putString("update_time", DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss")); 95editor.commit(); 96showToast("数据已写入共享参数"); 97} 98} 99 100private void showToast(String desc) { 101Toast.makeText(this, desc, Toast.LENGTH_SHORT).show(); 102} 103 104public static void startHome(Context mContext) { 105Intent intent = new Intent(mContext, class_4_1_1.class); 106mContext.startActivity(intent); 107} 108 109 110 }

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:focusable="true" 5android:focusableInTouchMode="true" 6android:orientation="vertical" 7android:padding="10dp" > 8 9< RelativeLayout 10android:layout_width="match_parent" 11android:layout_height="50dp" > 12 13< TextView 14android:id="@+id/tv_name" 15android:layout_width="wrap_content" 16android:layout_height="match_parent" 17android:layout_alignParentLeft="true" 18android:gravity="center" 19android:text="姓名:" 20android:textColor="@color/black" 21android:textSize="17sp" /> 22 23< EditText 24android:id="@+id/et_name" 25android:layout_width="match_parent" 26android:layout_height="match_parent" 27android:layout_marginBottom="5dp" 28android:layout_marginTop="5dp" 29android:layout_toRightOf="@+id/tv_name" 30android:background="@drawable/editext_selector" 31android:gravity="left|center" 32android:hint="请输入姓名" 33android:inputType="text" 34android:maxLength="12" 35android:textColor="@color/black" 36android:textColorHint="@color/grey" 37android:textCursorDrawable="@drawable/text_cursor" 38android:textSize="17sp" /> 39< /RelativeLayout> 40 41< RelativeLayout 42android:layout_width="match_parent" 43android:layout_height="50dp" > 44 45< TextView 46android:id="@+id/tv_age" 47android:layout_width="wrap_content" 48android:layout_height="match_parent" 49android:layout_alignParentLeft="true" 50android:gravity="center" 51android:text="年龄:" 52android:textColor="@color/black" 53android:textSize="17sp" /> 54 55< EditText 56android:id="@+id/et_age" 57android:layout_width="match_parent" 58android:layout_height="match_parent" 59android:layout_marginBottom="5dp" 60android:layout_marginTop="5dp" 61android:layout_toRightOf="@+id/tv_age" 62android:background="@drawable/editext_selector" 63android:gravity="left|center" 64android:hint="请输入年龄" 65android:inputType="number" 66android:maxLength="2" 67android:textColor="@color/black" 68android:textColorHint="@color/grey" 69android:textCursorDrawable="@drawable/text_cursor" 70android:textSize="17sp" /> 71< /RelativeLayout> 72 73< RelativeLayout 74android:layout_width="match_parent" 75android:layout_height="50dp" > 76 77< TextView 78android:id="@+id/tv_height" 79android:layout_width="wrap_content" 80android:layout_height="match_parent" 81android:layout_alignParentLeft="true" 82android:gravity="center" 83android:text="身高:" 84android:textColor="@color/black" 85android:textSize="17sp" /> 86 87< EditText 88android:id="@+id/et_height" 89android:layout_width="match_parent" 90android:layout_height="match_parent" 91android:layout_marginBottom="5dp" 92android:layout_marginTop="5dp" 93android:layout_toRightOf="@+id/tv_height" 94android:background="@drawable/editext_selector" 95android:gravity="left|center" 96android:hint="请输入身高" 97android:inputType="number" 98android:maxLength="3" 99android:textColor="@color/black" 100android:textColorHint="@color/grey" 101android:textCursorDrawable="@drawable/text_cursor" 102android:textSize="17sp" /> 103< /RelativeLayout> 104 105< RelativeLayout 106android:layout_width="match_parent" 107android:layout_height="50dp" > 108 109< TextView 110android:id="@+id/tv_weight" 111android:layout_width="wrap_content" 112android:layout_height="match_parent" 113android:layout_alignParentLeft="true" 114android:gravity="center" 115android:text="体重:" 116android:textColor="@color/black" 117android:textSize="17sp" /> 118 119< EditText 120android:id="@+id/et_weight" 121android:layout_width="match_parent" 122android:layout_height="match_parent" 123android:layout_marginBottom="5dp" 124android:layout_marginTop="5dp" 125android:layout_toRightOf="@+id/tv_weight" 126android:background="@drawable/editext_selector" 127android:gravity="left|center" 128android:hint="请输入体重" 129android:inputType="numberDecimal" 130android:maxLength="5" 131android:textColor="@color/black" 132android:textColorHint="@color/grey" 133android:textCursorDrawable="@drawable/text_cursor" 134android:textSize="17sp" /> 135< /RelativeLayout> 136 137< RelativeLayout 138android:layout_width="match_parent" 139android:layout_height="50dp" > 140 141< TextView 142android:id="@+id/tv_married" 143android:layout_width="wrap_content" 144android:layout_height="match_parent" 145android:layout_alignParentLeft="true" 146android:gravity="center" 147android:text="婚否:" 148android:textColor="@color/black" 149android:textSize="17sp" /> 150 151< Spinner 152android:id="@+id/sp_married" 153android:layout_width="match_parent" 154android:layout_height="match_parent" 155android:layout_toRightOf="@+id/tv_married" 156android:gravity="left|center" 157android:spinnerMode="dialog" /> 158< /RelativeLayout> 159 160< Button 161android:id="@+id/btn_save" 162android:layout_width="match_parent" 163android:layout_height="wrap_content" 164android:text="保存到共享参数" 165android:textColor="@color/black" 166android:textSize="20sp" /> 167 168 < /LinearLayout>

Android 开发笔记___存储方式__共享参数__sharedprefences

文章图片

 
read
1 package com.example.alimjan.hello_world; 2 3 import java.util.Map; 4 5 /** 6* Created by alimjan on 7/4/2017. 7*/ 8 9 import android.content.Context; 10 import android.content.Intent; 11 import android.content.SharedPreferences; 12import android.os.Bundle; 13import android.support.v7.app.AppCompatActivity; 14import android.widget.TextView; 15 16 public class class_4_1_1_1 extends AppCompatActivity { 17 18private SharedPreferences mShared; 19private TextView tv_share; 20 21@Override 22protected void onCreate(Bundle savedInstanceState) { 23super.onCreate(savedInstanceState); 24setContentView(R.layout.code_4_1_1_1); 25tv_share = (TextView) findViewById(R.id.tv_share); 26readSharedPreferences(); 27} 28 29private void readSharedPreferences() { 30mShared = getSharedPreferences("share", MODE_PRIVATE); 31String desc = "共享参数中保存的信息如下:"; 32Map< String, Object> mapParam = (Map< String, Object> ) mShared.getAll(); 33for (Map.Entry< String, Object> item_map : mapParam.entrySet()) { 34String key = item_map.getKey(); 35Object value = https://www.songbingjia.com/android/item_map.getValue(); 36if (value instanceof String) { 37desc = String.format("%s\\n %s的取值为%s", desc, key, 38mShared.getString(key, "")); 39} else if (value instanceof Integer) { 40desc = String.format("%s\\n %s的取值为%d", desc, key, 41mShared.getInt(key, 0)); 42} else if (value instanceof Float) { 43desc = String.format("%s\\n %s的取值为%f", desc, key, 44mShared.getFloat(key, 0.0f)); 45} else if (value instanceof Boolean) { 46desc = String.format("%s\\n %s的取值为%b", desc, key, 47mShared.getBoolean(key, false)); 48} else if (value instanceof Long) { 49desc = String.format("%s\\n %s的取值为%d", desc, key, 50mShared.getLong(key, 0l)); 51} else { 52desc = String.format("%s\\n参数%s的取值为未知类型", desc, key); 53} 54} 55if (mapParam==null || mapParam.size()< =0) { 56desc = "共享参数中保存的信息为空"; 57} 58tv_share.setText(desc); 59} 60 61public static void startHome(Context mContext) { 62Intent intent = new Intent(mContext, class_4_1_1_1.class); 63mContext.startActivity(intent); 64} 65 66 }

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:focusable="true" 5android:focusableInTouchMode="true" 6android:orientation="vertical" 7android:padding="10dp" > 8 9< TextView 10android:id="@+id/tv_share" 11android:layout_width="match_parent" 12android:layout_height="wrap_content" 13android:textColor="@color/black" 14android:textSize="17sp" /> 15 16 < /LinearLayout>

【Android 开发笔记___存储方式__共享参数__sharedprefences】 

    推荐阅读