• Android源码分析(十四)----如何使用SharedPreferencce保存数据


    一:SharedPreference如何使用

    此文章只是提供一种数据保存的方式, 具体使用场景请根据需求情况自行调整。
    EditText添加saveData点击事件, 保存数据。

    diff --git a/res/layout/activity_voicemail_settings.xml b/res/layout/activity_voicemail_settings.xml
    index cd7af05..57526f2 100755
    --- a/res/layout/activity_voicemail_settings.xml
    +++ b/res/layout/activity_voicemail_settings.xml
    @@ -29,6 +29,7 @@
             android:imeOptions="flagForceAscii|actionDone"
             android:includeFontPadding="true"
             android:inputType="text"
    +        android:onClick="saveData"
             android:paddingLeft="@dimen/focus_item_padding_x"
             android:singleLine="true"
             android:textColor="@color/lv_focused"
    
    1. 通过getSharedPreferences("voicemail_data", MODE_PRIVATE);获取SharedPreferen对象。
    2. sharedPreferences.edit();获取editor对象。
    3. initData()初始化EditText数据。
    4. saveData()保存输入数据。
    5. mEditText.setSelection(mEditText.getText().length());此方法主要设置EditText光标位置。
    diff --git a/src/com/android/settings/VoicemailSettingsActivity.java b/src/com/android/settings/VoicemailSettingsActivity.java
    index 9babb18..58e929a 100755
    --- a/src/com/android/settings/VoicemailSettingsActivity.java
    +++ b/src/com/android/settings/VoicemailSettingsActivity.java
    @@ -4,6 +4,7 @@ import android.app.Activity;
     import android.content.ComponentName;
     import android.content.Context;
     import android.content.Intent;
    +import android.content.SharedPreferences;
     import android.content.pm.PackageManager;
     import android.content.pm.ResolveInfo;
     import android.os.Bundle;
    @@ -12,6 +13,7 @@ import android.view.KeyEvent;
     import android.view.View;
     import android.view.Window;
     import android.widget.EditText;
    +import android.widget.Toast;
     
     import java.util.List;
     
    @@ -22,6 +24,10 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
         private int mSettingsFocusHeight;
         private int mSettingsNormalHeight;
         private int mSettingsStartY;
    +    private SharedPreferences sharedPreferences;
    +    private SharedPreferences.Editor editor;
    +    private String key;
    +    private String result;
     
         @Override
         protected void onCreate(Bundle savedInstanceState) {
    @@ -33,7 +39,12 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
             for (int i = 0; i < ids.length; i++) {
                 strs[i] = getString(ids[i]);
             }
    +        sharedPreferences = getSharedPreferences("voicemail_data", MODE_PRIVATE);
    +        editor = sharedPreferences.edit();
             mEditText = (EditText) findViewById(R.id.input_number);
    +        initData();
    +        mEditText.setSelection(mEditText.getText().length());//光标行尾
             mSettingsRecyclerView = (SettingsRecyclerView) findViewById(R.id.voicemail_list_view);
             mSettingsAdapter = new SettingsAdapter(this, strs, this);
             mSettingsRecyclerView.setLayoutManager(mSettingsAdapter.getLayoutManager());
    @@ -45,6 +56,25 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
             mSettingsRecyclerView.setHasFixedSize(true);
         }
     
    +    private void initData() {
    +        result = sharedPreferences.getString(key, null);
    +        if (result == null) {
    +            mEditText.setText("");
    +        } else {
    +            mEditText.setText(result);
    +        }
    +    }
    +
    +    public void saveData(View view) {
    +        editor.remove(key);
    +        String value = mEditText.getText().toString().trim();
    +        editor.putString(key, value);
    +        editor.commit();
    +        Toast.makeText(VoicemailSettingsActivity.this, "save success", Toast.LENGTH_SHORT).show();
    +        finish();
    +    }
    +
    +
         @Override
         public void onClick(View view) {
             Log.d("jasun", "=============onClick============");
    (END)
    

    喜欢源码分析系列可参考其他文章:
    Android源码分析(一)-----如何快速掌握Android编译文件
    Android源码分析(二)-----如何编译修改后的framework资源文件
    Android源码分析(三)-----系统框架设计思想
    Android源码分析(四)-----Android源码编译及刷机步骤
    Android源码分析(五)-----如何从架构师的角度去设计Framework框架

  • 相关阅读:
    Android APN开关原理
    HTC Wildfire 闪光灯手电筒的实现方式
    Android Stagefright MPEG4Extractor分析
    Ubuntu 10.10 64bit下使用Android NDK r6编译FFmpeg 0.8.1
    开始IMS学习相关名词和术语(持续更新)
    Ubuntu 下安装代码格式化程序 astyle
    Ubuntu 10.10 x64 安装 ffmpeg tutorial 相关支持库
    关于GradientDrawable的angle属性
    判断SIM卡所属的运营商
    最小生成树算法
  • 原文地址:https://www.cnblogs.com/ljx646566715/p/10893956.html
Copyright © 2020-2023  润新知