SharedPreference Demo

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:andro>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 }



相关文章:

  • 2022-01-06
  • 2021-10-16
  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2021-08-21
  • 2022-01-20
  • 2021-09-21
猜你喜欢
  • 2021-12-14
  • 2022-12-23
  • 2021-11-04
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案