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 7 import android.content.Context; 8 import android.content.Intent; 9 import android.content.SharedPreferences; 10 import android.os.Bundle; 11 import android.support.v7.app.AppCompatActivity; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.AdapterView; 15 import android.widget.ArrayAdapter; 16 import android.widget.EditText; 17 import android.widget.Spinner; 18 import android.widget.Toast; 19 import android.widget.AdapterView.OnItemSelectedListener; 20 21 import com.example.alimjan.hello_world.Utils.DateUtil; 22 23 public class class_4_1_1 extends AppCompatActivity implements OnClickListener { 24 25 private SharedPreferences mShared; 26 private EditText et_name; 27 private EditText et_age; 28 private EditText et_height; 29 private EditText et_weight; 30 private boolean bMarried = false; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.code_4_1_1); 36 et_name = (EditText) findViewById(R.id.et_name); 37 et_age = (EditText) findViewById(R.id.et_age); 38 et_height = (EditText) findViewById(R.id.et_height); 39 et_weight = (EditText) findViewById(R.id.et_weight); 40 findViewById(R.id.btn_save).setOnClickListener(this); 41 42 ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, 43 R.layout.item_select, typeArray); 44 typeAdapter.setDropDownViewResource(R.layout.item_dropdown); 45 Spinner sp_married = (Spinner) findViewById(R.id.sp_married); 46 sp_married.setPrompt("请选择婚姻状况"); 47 sp_married.setAdapter(typeAdapter); 48 sp_married.setSelection(0); 49 sp_married.setOnItemSelectedListener(new TypeSelectedListener()); 50 51 mShared = getSharedPreferences("share", MODE_PRIVATE); 52 } 53 54 private String[] typeArray = {"未婚", "已婚"}; 55 class TypeSelectedListener implements OnItemSelectedListener { 56 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 57 bMarried = (arg2==0)?false:true; 58 } 59 60 public void onNothingSelected(AdapterView<?> arg0) { 61 } 62 } 63 64 @Override 65 public void onClick(View v) { 66 if (v.getId() == R.id.btn_save) { 67 String name = et_name.getText().toString(); 68 String age = et_age.getText().toString(); 69 String height = et_height.getText().toString(); 70 String weight = et_weight.getText().toString(); 71 if (name==null || name.length()<=0) { 72 showToast("请先填写姓名"); 73 return; 74 } 75 if (age==null || age.length()<=0) { 76 showToast("请先填写年龄"); 77 return; 78 } 79 if (height==null || height.length()<=0) { 80 showToast("请先填写身高"); 81 return; 82 } 83 if (weight==null || weight.length()<=0) { 84 showToast("请先填写体重"); 85 return; 86 } 87 88 SharedPreferences.Editor editor = mShared.edit(); 89 editor.putString("name", name); 90 editor.putInt("age", Integer.parseInt(age)); 91 editor.putLong("height", Long.parseLong(height)); 92 editor.putFloat("weight", Float.parseFloat(weight)); 93 editor.putBoolean("married", bMarried); 94 editor.putString("update_time", DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss")); 95 editor.commit(); 96 showToast("数据已写入共享参数"); 97 } 98 } 99 100 private void showToast(String desc) { 101 Toast.makeText(this, desc, Toast.LENGTH_SHORT).show(); 102 } 103 104 public static void startHome(Context mContext) { 105 Intent intent = new Intent(mContext, class_4_1_1.class); 106 mContext.startActivity(intent); 107 } 108 109 110 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:focusable="true" 5 android:focusableInTouchMode="true" 6 android:orientation="vertical" 7 android:padding="10dp" > 8 9 <RelativeLayout 10 android:layout_width="match_parent" 11 android:layout_height="50dp" > 12 13 <TextView 14 android:id="@+id/tv_name" 15 android:layout_width="wrap_content" 16 android:layout_height="match_parent" 17 android:layout_alignParentLeft="true" 18 android:gravity="center" 19 android:text="姓名:" 20 android:textColor="@color/black" 21 android:textSize="17sp" /> 22 23 <EditText 24 android:id="@+id/et_name" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:layout_marginBottom="5dp" 28 android:layout_marginTop="5dp" 29 android:layout_toRightOf="@+id/tv_name" 30 android:background="@drawable/editext_selector" 31 android:gravity="left|center" 32 android:hint="请输入姓名" 33 android:inputType="text" 34 android:maxLength="12" 35 android:textColor="@color/black" 36 android:textColorHint="@color/grey" 37 android:textCursorDrawable="@drawable/text_cursor" 38 android:textSize="17sp" /> 39 </RelativeLayout> 40 41 <RelativeLayout 42 android:layout_width="match_parent" 43 android:layout_height="50dp" > 44 45 <TextView 46 android:id="@+id/tv_age" 47 android:layout_width="wrap_content" 48 android:layout_height="match_parent" 49 android:layout_alignParentLeft="true" 50 android:gravity="center" 51 android:text="年龄:" 52 android:textColor="@color/black" 53 android:textSize="17sp" /> 54 55 <EditText 56 android:id="@+id/et_age" 57 android:layout_width="match_parent" 58 android:layout_height="match_parent" 59 android:layout_marginBottom="5dp" 60 android:layout_marginTop="5dp" 61 android:layout_toRightOf="@+id/tv_age" 62 android:background="@drawable/editext_selector" 63 android:gravity="left|center" 64 android:hint="请输入年龄" 65 android:inputType="number" 66 android:maxLength="2" 67 android:textColor="@color/black" 68 android:textColorHint="@color/grey" 69 android:textCursorDrawable="@drawable/text_cursor" 70 android:textSize="17sp" /> 71 </RelativeLayout> 72 73 <RelativeLayout 74 android:layout_width="match_parent" 75 android:layout_height="50dp" > 76 77 <TextView 78 android:id="@+id/tv_height" 79 android:layout_width="wrap_content" 80 android:layout_height="match_parent" 81 android:layout_alignParentLeft="true" 82 android:gravity="center" 83 android:text="身高:" 84 android:textColor="@color/black" 85 android:textSize="17sp" /> 86 87 <EditText 88 android:id="@+id/et_height" 89 android:layout_width="match_parent" 90 android:layout_height="match_parent" 91 android:layout_marginBottom="5dp" 92 android:layout_marginTop="5dp" 93 android:layout_toRightOf="@+id/tv_height" 94 android:background="@drawable/editext_selector" 95 android:gravity="left|center" 96 android:hint="请输入身高" 97 android:inputType="number" 98 android:maxLength="3" 99 android:textColor="@color/black" 100 android:textColorHint="@color/grey" 101 android:textCursorDrawable="@drawable/text_cursor" 102 android:textSize="17sp" /> 103 </RelativeLayout> 104 105 <RelativeLayout 106 android:layout_width="match_parent" 107 android:layout_height="50dp" > 108 109 <TextView 110 android:id="@+id/tv_weight" 111 android:layout_width="wrap_content" 112 android:layout_height="match_parent" 113 android:layout_alignParentLeft="true" 114 android:gravity="center" 115 android:text="体重:" 116 android:textColor="@color/black" 117 android:textSize="17sp" /> 118 119 <EditText 120 android:id="@+id/et_weight" 121 android:layout_width="match_parent" 122 android:layout_height="match_parent" 123 android:layout_marginBottom="5dp" 124 android:layout_marginTop="5dp" 125 android:layout_toRightOf="@+id/tv_weight" 126 android:background="@drawable/editext_selector" 127 android:gravity="left|center" 128 android:hint="请输入体重" 129 android:inputType="numberDecimal" 130 android:maxLength="5" 131 android:textColor="@color/black" 132 android:textColorHint="@color/grey" 133 android:textCursorDrawable="@drawable/text_cursor" 134 android:textSize="17sp" /> 135 </RelativeLayout> 136 137 <RelativeLayout 138 android:layout_width="match_parent" 139 android:layout_height="50dp" > 140 141 <TextView 142 android:id="@+id/tv_married" 143 android:layout_width="wrap_content" 144 android:layout_height="match_parent" 145 android:layout_alignParentLeft="true" 146 android:gravity="center" 147 android:text="婚否:" 148 android:textColor="@color/black" 149 android:textSize="17sp" /> 150 151 <Spinner 152 android:id="@+id/sp_married" 153 android:layout_width="match_parent" 154 android:layout_height="match_parent" 155 android:layout_toRightOf="@+id/tv_married" 156 android:gravity="left|center" 157 android:spinnerMode="dialog" /> 158 </RelativeLayout> 159 160 <Button 161 android:id="@+id/btn_save" 162 android:layout_width="match_parent" 163 android:layout_height="wrap_content" 164 android:text="保存到共享参数" 165 android:textColor="@color/black" 166 android:textSize="20sp" /> 167 168 </LinearLayout>
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; 12 import android.os.Bundle; 13 import android.support.v7.app.AppCompatActivity; 14 import android.widget.TextView; 15 16 public class class_4_1_1_1 extends AppCompatActivity { 17 18 private SharedPreferences mShared; 19 private TextView tv_share; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.code_4_1_1_1); 25 tv_share = (TextView) findViewById(R.id.tv_share); 26 readSharedPreferences(); 27 } 28 29 private void readSharedPreferences() { 30 mShared = getSharedPreferences("share", MODE_PRIVATE); 31 String desc = "共享参数中保存的信息如下:"; 32 Map<String, Object> mapParam = (Map<String, Object>) mShared.getAll(); 33 for (Map.Entry<String, Object> item_map : mapParam.entrySet()) { 34 String key = item_map.getKey(); 35 Object value = item_map.getValue(); 36 if (value instanceof String) { 37 desc = String.format("%s %s的取值为%s", desc, key, 38 mShared.getString(key, "")); 39 } else if (value instanceof Integer) { 40 desc = String.format("%s %s的取值为%d", desc, key, 41 mShared.getInt(key, 0)); 42 } else if (value instanceof Float) { 43 desc = String.format("%s %s的取值为%f", desc, key, 44 mShared.getFloat(key, 0.0f)); 45 } else if (value instanceof Boolean) { 46 desc = String.format("%s %s的取值为%b", desc, key, 47 mShared.getBoolean(key, false)); 48 } else if (value instanceof Long) { 49 desc = String.format("%s %s的取值为%d", desc, key, 50 mShared.getLong(key, 0l)); 51 } else { 52 desc = String.format("%s 参数%s的取值为未知类型", desc, key); 53 } 54 } 55 if (mapParam==null || mapParam.size()<=0) { 56 desc = "共享参数中保存的信息为空"; 57 } 58 tv_share.setText(desc); 59 } 60 61 public static void startHome(Context mContext) { 62 Intent intent = new Intent(mContext, class_4_1_1_1.class); 63 mContext.startActivity(intent); 64 } 65 66 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:focusable="true" 5 android:focusableInTouchMode="true" 6 android:orientation="vertical" 7 android:padding="10dp" > 8 9 <TextView 10 android:id="@+id/tv_share" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:textColor="@color/black" 14 android:textSize="17sp" /> 15 16 </LinearLayout>