一: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"
- 通过
getSharedPreferences("voicemail_data", MODE_PRIVATE);
获取SharedPreferen对象。 sharedPreferences.edit();
获取editor对象。initData()
初始化EditText数据。saveData()
保存输入数据。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框架