android用sharepreference保存输入框中的内容


  1. package com.cia.settings;
  2. import android.app.Activity;
  3. import android.content.SharedPreferences;
  4. import android.content.SharedPreferences.Editor;
  5. import android.os.Bundle;
  6. import android.view.KeyEvent;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. public class MainActivity extends Activity
  10. {
  11. private EditText et_server,et_backup,et_upgrade;
  12. private TextView  tv_show_version,tv_show_mac;
  13. private SharedPreferences sharedPrefrences;
  14. private Editor editor;
  15.   // 要存储的文件名
  16.   private static final String FILENAME = "filename";
  17. @Override
  18. public void onCreate(Bundle savedInstanceState)
  19. {
  20.   super.onCreate(savedInstanceState);
  21.   setContentView(R.layout.main);
  22.   et_server = (EditText) findViewById(R.id.edit_server);
  23.   et_backup = (EditText) findViewById(R.id.edit_backup);
  24.   et_upgrade = (EditText) findViewById(R.id.edit_upgrade);
  25.   
  26.     sharedPrefrences = this.getSharedPreferences(FILENAME, MODE_WORLD_READABLE);
  27.    
  28.     
  29.     String r_server = sharedPrefrences.getString("server", "");
  30.     String r_backup = sharedPrefrences.getString("backup", "");
  31.     String r_upgrade= sharedPrefrences.getString("upgrade", "");
  32.     et_server.setText(r_server);
  33.     et_backup.setText(r_backup);
  34.     et_upgrade.setText(r_upgrade);
  35.     
  36.     
  37. }

  38. //在点击退出时保存数据
  39. @Override
  40. public boolean onKeyDown(int keyCode, KeyEvent event)
  41. {
  42.   // 得到编辑器对象
  43.    editor = getSharedPreferences(FILENAME, MODE_WORLD_WRITEABLE).edit();
  44.   if(keyCode==KeyEvent.KEYCODE_BACK)
  45.   {
  46.    String server=et_server.getText().toString();
  47.    String backup=et_backup.getText().toString();
  48.    String upgrade=et_upgrade.getText().toString();
  49.    editor.putString("server", server);
  50.    editor.putString("backup", backup);
  51.    editor.putString("upgrade", upgrade);
  52.    editor.commit();
  53.   }
  54.   return super.onKeyDown(keyCode, event);
  55. }
复制代码
第二种方法, if class extends frame.


  1. package com.android.settings;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import android.app.Fragment;
  8. import android.content.Context;
  9. import android.content.SharedPreferences;
  10. import android.content.SharedPreferences.Editor;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.os.SystemProperties;
  14. import android.util.Log;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.ViewGroup;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.TextView;
  22. public class JumpTvSettings extends Fragment implements OnClickListener
  23. {
  24.     private EditText et_server, et_backup, et_upgrade;
  25.     private TextView tv_show_version, tv_show_mac;
  26.     private Editor editor;
  27.     private Button btn;
  28.     
  29.     private static final String TAG = "JumpTvSettings_mac";
  30.     private static final String LOG_TAG="jumTv_Settings_version";
  31.     
  32.      private SharedPreferences mPrefs;
  33.     // 要存储的文件名
  34.     private static final String FILENAME = "filename_jumpTv";
  35.     
  36.     private static final  String CONFIG_PATH = "/sys/class/efuse/mac";
  37.     private static final String FILENAME_PROC_VERSION = "/proc/version";
  38.     
  39.     private static final String KEY_FIRMWARE_VERSION = "firmware_version";
  40.     
  41.     @Override
  42.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  43.             Bundle savedInstanceState)
  44.     {
  45.         View v=inflater.inflate(R.layout.jump_page, container, false);
  46.         et_server = (EditText)v.findViewById(R.id.edit_server);
  47.         et_backup = (EditText)v.findViewById(R.id.edit_backup);
  48.         et_upgrade = (EditText)v.findViewById(R.id.edit_upgrade);
  49.         btn=(Button)v.findViewById(R.id.btn_submit);
  50.         btn.setOnClickListener(this);
  51.         et_upgrade = (EditText)v.findViewById(R.id.edit_upgrade);
  52.                                                 tv_show_version = (TextView)v.findViewById(R.id.txt_show_version);
  53.          tv_show_mac = (TextView)v.findViewById(R.id.txt_show_mac);
  54.         mPrefs =  getActivity().getSharedPreferences(FILENAME, Context.MODE_WORLD_READABLE);
  55.           String r_server = mPrefs.getString("server", "");
  56.           String r_backup = mPrefs.getString("backup", "");
  57.           String r_upgrade= mPrefs.getString("upgrade", "");
  58.           et_server.setText(r_server);
  59.           et_backup.setText(r_backup);
  60.           et_upgrade.setText(r_upgrade);
  61.             tv_show_mac.setText(new StringBuilder(this.getEthMacfromEfuse()).toString());
  62.             tv_show_version.setText(Build.VERSION.RELEASE);
  63.             tv_show_version.setEnabled(true);
  64.          return v;
  65.     }

  66.      /**
  67.          * Reads a line from the specified file.
  68.          * @param filename the file to read from
  69.          * [url=home.php?mod=space&uid=7300]@return[/url] the first line, if any.
  70.          * @throws IOException if the file couldn't be read
  71.          */
  72.         private String readLine(String filename) throws IOException {
  73.             BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
  74.             try {
  75.                 return reader.readLine();
  76.             } finally {
  77.                 reader.close();
  78.             }
  79.         }
  80.     private String getFormattedKernelVersion() {
  81.         String procVersionStr;
  82.         try {
  83.             procVersionStr = readLine(FILENAME_PROC_VERSION);
  84.             final String PROC_VERSION_REGEX =
  85.                 "[url=file://w+//s][color=#0066cc]\\w+\\s[/color][/url]+" + /* ignore: Linux */
  86.                 "[url=file://w+//s][color=#0066cc]\\w+\\s[/color][/url]+" + /* ignore: version */
  87.                 "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
  88.                 "错误!超链接引用无效。" + /* group 2: ([url=mailto:xxxxxx@xxxxx.constant][color=#0066cc]xxxxxx@xxxxx.constant[/color][/url]) */
  89.                 "错误!超链接引用无效。" + /* ignore: (gcc ..) */
  90.                 "([^\\s]+)\\s+" + /* group 3: #26 */
  91.                 "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
  92.                 "(.+)"; /* group 4: date */
  93.             Pattern p = Pattern.compile(PROC_VERSION_REGEX);
  94.             Matcher m = p.matcher(procVersionStr);
  95.             if (!m.matches()) {
  96.                 Log.e(LOG_TAG, "Regex did not match on /proc/version: " + procVersionStr);
  97.                 return "Unavailable";
  98.             } else if (m.groupCount() < 4) {
  99.                 Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
  100.                         + " groups");
  101.                 return "Unavailable";
  102.             } else {
  103.                 return (new StringBuilder(m.group(1)).append("\n").append(
  104.                         m.group(2)).append(" ").append(m.group(3)).append("\n")
  105.                         .append(m.group(4))).toString();
  106.             }
  107.         } catch (IOException e) {
  108.             Log.e(LOG_TAG,
  109.                 "IO Exception when getting kernel version for Device Info screen",
  110.                 e);
  111.             return "Unavailable";
  112.         }
  113.     }
  114.     // 获取mac地址
  115.     private String getEthMacfromEfuse()
  116.     {
  117.         String sn = null;
  118.         try
  119.         {
  120.             BufferedReader reader = new BufferedReader(new FileReader(CONFIG_PATH), 12);
  121.             try
  122.             {
  123.                 sn = reader.readLine();
  124.             }finally
  125.             {
  126.                 reader.close();
  127.             }
  128.             Log.d(TAG, "/sys/class/efuse/mac: " + sn);
  129.             if (sn.equals("00:00:00:00:00:00"))
  130.                 return SystemProperties.get("ubootenv.var.ethaddr",getString(R.string.status_unavailable));
  131.             else
  132.                 return sn;
  133.         } catch (IOException e)
  134.         {
  135.             Log.e(TAG,"IO Exception when getting serial number for Device Info screen",e);
  136.             // return "null";
  137.             return SystemProperties.get("ubootenv.var.ethaddr",getString(R.string.status_unavailable));
  138.         }
  139.     }
  140.     
  141. @Override
  142.   public void onPause()
  143.   {
  144.       //界面失去控制权时保存数据
  145.       editor =getActivity().getSharedPreferences(FILENAME, Context.MODE_WORLD_WRITEABLE).edit();
  146.       String server = et_server.getText().toString();
  147.       String backup = et_backup.getText().toString();
  148.       String upgrade = et_upgrade.getText().toString();
  149.       editor.putString("server", server);
  150.       editor.putString("backup", backup);
  151.       editor.putString("upgrade", upgrade);
  152.       editor.commit();
  153.           super.onPause();
  154.   }
  155.     
  156.     public void onSaveInstanceState(Bundle outState) {
  157.         //界面销毁之前保存数据
  158.         super.onSaveInstanceState(outState);
  159.         outState.putString("server", et_server.getText().toString());
  160.         outState.putString("server", et_backup.getText().toString());
  161.         outState.putString("server", et_upgrade.getText().toString());
  162.     }
  163. }

  164. /*
  165. * 取文件中的值
  166. */
  167.    public void onCreate(Bundle savedInstanceState) {
  168.         super.onCreate(savedInstanceState);
  169.         Context context = null;
  170.         try {
  171.             context = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY);
  172.         } catch (NameNotFoundException e) {
  173.             // TODO Auto-generated catch block
  174.             e.printStackTrace();
  175.         }
  176.         SharedPreferences settings = context.getSharedPreferences("filename_jumpTv",
  177.                 Context.MODE_WORLD_READABLE);
  178.         String server = settings.getString("server", " ");
  179.         String backup = settings.getString("backup", " ");
  180.         Log.d("SharedPreferences", "server:" + server + ", backup:" + backup);
  181.         Log.d("SharedPreferences", "获取数据成功");
  182.         Uri server_uri = Uri.parse(server);
  183.         Uri backup_uri = Uri.parse(backup);
  184.         Intent intent1 = new Intent(Intent.ACTION_VIEW, server_uri);
  185.         Intent intent2 = new Intent(Intent.ACTION_VIEW, backup_uri);
  186.         startActivity(intent1);
  187.         startActivity(intent2);
  188.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值