SharedPreference Demo
生活随笔
收集整理的這篇文章主要介紹了
SharedPreference Demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <EditText
7 android:id="@+id/edit"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:layout_margin="15dip"
11 />
12 <Button
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="保存短信"
16 android:layout_gravity="right"
17 android:layout_margin="15dip"
18 />
19 </LinearLayout>
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.widget.EditText;
7
8 public class SaveDataActivity extends Activity {
9 private EditText editText;
10 private static final String TEMP_SMS = "temp_sms";
11 /** Called when the activity is first created. */
12 @Override
13 public void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.sharepreference_1);
16 editText = (EditText)findViewById(R.id.edit);
17 SharedPreferences sharedPreferences = getSharedPreferences(TEMP_SMS,MODE_WORLD_WRITEABLE);
18 String content = sharedPreferences.getString("sms_content", "");
19 editText.setText(content);
20 }
21 @Override
22 protected void onStop() {
23 super.onStop();
24 SharedPreferences.Editor editor = getSharedPreferences(TEMP_SMS,MODE_WORLD_WRITEABLE).edit();
25 // 將EditText中的文字添加到編輯器
26 editor.putString("sms_content", editText.getText().toString());
27 editor.commit();
28 }
29
30 }
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <EditText
7 android:id="@+id/edit"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:layout_margin="15dip"
11 />
12 <Button
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="保存短信"
16 android:layout_gravity="right"
17 android:layout_margin="15dip"
18 />
19 </LinearLayout>
?
1 package com.turboradio.activity;2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.widget.EditText;
7
8 public class SaveDataActivity extends Activity {
9 private EditText editText;
10 private static final String TEMP_SMS = "temp_sms";
11 /** Called when the activity is first created. */
12 @Override
13 public void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.sharepreference_1);
16 editText = (EditText)findViewById(R.id.edit);
17 SharedPreferences sharedPreferences = getSharedPreferences(TEMP_SMS,MODE_WORLD_WRITEABLE);
18 String content = sharedPreferences.getString("sms_content", "");
19 editText.setText(content);
20 }
21 @Override
22 protected void onStop() {
23 super.onStop();
24 SharedPreferences.Editor editor = getSharedPreferences(TEMP_SMS,MODE_WORLD_WRITEABLE).edit();
25 // 將EditText中的文字添加到編輯器
26 editor.putString("sms_content", editText.getText().toString());
27 editor.commit();
28 }
29
30 }
轉載于:https://www.cnblogs.com/jiayonghua/archive/2011/12/14/2287291.html
總結
以上是生活随笔為你收集整理的SharedPreference Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20佳带给你灵感的优秀个人博客设计案例
- 下一篇: Picasa——万能图片查看