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

Android 自动生成的文件
http://www.eoeandroid.com/thread-81948-1-1.html

SAX解析本地xml文件
http://www.eoeandroid.com/thread-92835-1-1.html

在android中生成xml文件例子
http://www.eoeandroid.com/thread-198223-1-1.html

 

package com.cia.settings;

 

import android.app.Activity;

 

import android.content.SharedPreferences;

 

import android.content.SharedPreferences.Editor;

 

import android.os.Bundle;

 

import android.view.KeyEvent;

 

import android.widget.EditText;

 

import android.widget.TextView;

 

public class MainActivity extends Activity

 

{

 

private EditText et_server,et_backup,et_upgrade;

 

private TextView  tv_show_version,tv_show_mac;

 

private SharedPreferences sharedPrefrences;

 

private Editor editor;

 

  // 要存储的文件名

 

  private static final String FILENAME = "filename";

 

@Override

 

public void onCreate(Bundle savedInstanceState)

 

{

 

  super.onCreate(savedInstanceState);

 

  setContentView(R.layout.main);

 

  et_server = (EditText) findViewById(R.id.edit_server);

 

  et_backup = (EditText) findViewById(R.id.edit_backup);

 

  et_upgrade = (EditText) findViewById(R.id.edit_upgrade);

 

  

 

    sharedPrefrences = this.getSharedPreferences(FILENAME, MODE_WORLD_READABLE);

 

   

 

    

    String r_server = sharedPrefrences.getString("server", "");

 

    String r_backup = sharedPrefrences.getString("backup", "");

 

    String r_upgrade= sharedPrefrences.getString("upgrade", "");

 

    et_server.setText(r_server);

 

    et_backup.setText(r_backup);

 

    et_upgrade.setText(r_upgrade);

 

    

    

}

 



//在点击退出时保存数据

 

@Override

 

public boolean onKeyDown(int keyCode, KeyEvent event)

 

{

 

  // 得到编辑器对象

 

   editor = getSharedPreferences(FILENAME, MODE_WORLD_WRITEABLE).edit();

 

  if(keyCode==KeyEvent.KEYCODE_BACK)

 

  {

 

   String server=et_server.getText().toString();

 

   String backup=et_backup.getText().toString();

 

   String upgrade=et_upgrade.getText().toString();

 

   editor.putString("server", server);

 

   editor.putString("backup", backup);

 

   editor.putString("upgrade", upgrade);

 

   editor.commit();

 

  }

 

  return super.onKeyDown(keyCode, event);

 

}

 

 第二种方法, if class extends frame

 

package com.android.settings;

 

import java.io.BufferedReader;

 

import java.io.FileReader;

 

import java.io.IOException;

 

import java.util.regex.Matcher;

 

import java.util.regex.Pattern;

 

import android.app.Fragment;

 

import android.content.Context;

 

import android.content.SharedPreferences;

 

import android.content.SharedPreferences.Editor;

 

import android.os.Build;

 

import android.os.Bundle;

 

import android.os.SystemProperties;

 

import android.util.Log;

 

import android.view.LayoutInflater;

 

import android.view.View;

 

import android.view.View.OnClickListener;

 

import android.view.ViewGroup;

 

import android.widget.Button;

 

import android.widget.EditText;

 

import android.widget.TextView;

 

public class JumpTvSettings extends Fragment implements OnClickListener

 

{

 

    private EditText et_server, et_backup, et_upgrade;

 

    private TextView tv_show_version, tv_show_mac;

 

    private Editor editor;

 

    private Button btn;

 

    

    private static final String TAG = "JumpTvSettings_mac";

 

    private static final String LOG_TAG="jumTv_Settings_version";

 

    

     private SharedPreferences mPrefs;

 

    // 要存储的文件名

 

    private static final String FILENAME = "filename_jumpTv";

 

    

    private static final  String CONFIG_PATH = "/sys/class/efuse/mac";

 

    private static final String FILENAME_PROC_VERSION = "/proc/version";

 

    

    private static final String KEY_FIRMWARE_VERSION = "firmware_version";

 

    

    @Override

 

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

 

            Bundle savedInstanceState)

 

    {

 

        View v=inflater.inflate(R.layout.jump_page, container, false);

 

        et_server = (EditText)v.findViewById(R.id.edit_server);

 

        et_backup = (EditText)v.findViewById(R.id.edit_backup);

 

        et_upgrade = (EditText)v.findViewById(R.id.edit_upgrade);

 

        btn=(Button)v.findViewById(R.id.btn_submit);

 

        btn.setOnClickListener(this);

 

        et_upgrade = (EditText)v.findViewById(R.id.edit_upgrade);

 

                                                tv_show_version = (TextView)v.findViewById(R.id.txt_show_version);

 

         tv_show_mac = (TextView)v.findViewById(R.id.txt_show_mac);

 

        mPrefs =  getActivity().getSharedPreferences(FILENAME, Context.MODE_WORLD_READABLE);

 

          String r_server = mPrefs.getString("server", "");

 

          String r_backup = mPrefs.getString("backup", "");

 

          String r_upgrade= mPrefs.getString("upgrade", "");

 

          et_server.setText(r_server);

 

          et_backup.setText(r_backup);

 

          et_upgrade.setText(r_upgrade);

 

            tv_show_mac.setText(new StringBuilder(this.getEthMacfromEfuse()).toString());

 

            tv_show_version.setText(Build.VERSION.RELEASE);

 

            tv_show_version.setEnabled(true);

 

         return v;

 

    }

 



     /**

 

         * Reads a line from the specified file.

 

         * @param filename the file to read from

 

         * [url=home.php?mod=space&uid=7300]@return[/url] the first line, if any.

 

         * @throws IOException if the file couldn't be read

 

         */

 

        private String readLine(String filename) throws IOException {

 

            BufferedReader reader = new BufferedReader(new FileReader(filename), 256);

 

            try {

 

                return reader.readLine();

 

            } finally {

 

                reader.close();

 

            }

 

        }

 

    private String getFormattedKernelVersion() {

 

        String procVersionStr;

 

        try {

 

            procVersionStr = readLine(FILENAME_PROC_VERSION);

 

            final String PROC_VERSION_REGEX =

 

                "[url=file://w+//s][color=#0066cc]\\w+\\s[/color][/url]+" + /* ignore: Linux */

 

                "[url=file://w+//s][color=#0066cc]\\w+\\s[/color][/url]+" + /* ignore: version */

 

                "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */

 

                "错误!超链接引用无效。" + /* group 2: ([url=mailto:[email protected]][color=#0066cc][email protected][/color][/url]) */

 

                "错误!超链接引用无效。" + /* ignore: (gcc ..) */

 

                "([^\\s]+)\\s+" + /* group 3: #26 */

 

                "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */

 

                "(.+)"; /* group 4: date */

 

            Pattern p = Pattern.compile(PROC_VERSION_REGEX);

 

            Matcher m = p.matcher(procVersionStr);

 

            if (!m.matches()) {

 

                Log.e(LOG_TAG, "Regex did not match on /proc/version: " + procVersionStr);

 

                return "Unavailable";

 

            } else if (m.groupCount() < 4) {

 

                Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()

 

                        + " groups");

 

                return "Unavailable";

 

            } else {

 

                return (new StringBuilder(m.group(1)).append("\n").append(

 

                        m.group(2)).append(" ").append(m.group(3)).append("\n")

 

                        .append(m.group(4))).toString();

 

            }

 

        } catch (IOException e) {

 

            Log.e(LOG_TAG,

 

                "IO Exception when getting kernel version for Device Info screen",

 

                e);

 

            return "Unavailable";

 

        }

 

    }

 

    // 获取mac地址

 

    private String getEthMacfromEfuse()

 

    {

 

        String sn = null;

 

        try

 

        {

 

            BufferedReader reader = new BufferedReader(new FileReader(CONFIG_PATH), 12);

 

            try

 

            {

 

                sn = reader.readLine();

 

            }finally

 

            {

 

                reader.close();

 

            }

 

            Log.d(TAG, "/sys/class/efuse/mac: " + sn);

 

            if (sn.equals("00:00:00:00:00:00"))

 

                return SystemProperties.get("ubootenv.var.ethaddr",getString(R.string.status_unavailable));

 

            else

 

                return sn;

 

        } catch (IOException e)

 

        {

 

            Log.e(TAG,"IO Exception when getting serial number for Device Info screen",e);

 

            // return "null";

 

            return SystemProperties.get("ubootenv.var.ethaddr",getString(R.string.status_unavailable));

 

        }

 

    }

 

    

@Override

 

  public void onPause()

 

  {

 

      //界面失去控制权时保存数据

 

      editor =getActivity().getSharedPreferences(FILENAME, Context.MODE_WORLD_WRITEABLE).edit();

 

      String server = et_server.getText().toString();

 

      String backup = et_backup.getText().toString();

 

      String upgrade = et_upgrade.getText().toString();

 

      editor.putString("server", server);

 

      editor.putString("backup", backup);

 

      editor.putString("upgrade", upgrade);

 

      editor.commit();

 

          super.onPause();

 

  }

 

    

    public void onSaveInstanceState(Bundle outState) {

 

        //界面销毁之前保存数据

 

        super.onSaveInstanceState(outState);

 

        outState.putString("server", et_server.getText().toString());

 

        outState.putString("server", et_backup.getText().toString());

 

        outState.putString("server", et_upgrade.getText().toString());

 

    }

 

}

 



/*

 

* 取文件中的值

 

*/

 

   public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        Context context = null;

 

        try {

 

            context = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY);

 

        } catch (NameNotFoundException e) {

 

            // TODO Auto-generated catch block

 

            e.printStackTrace();

 

        }

 

        SharedPreferences settings = context.getSharedPreferences("filename_jumpTv",

 

                Context.MODE_WORLD_READABLE);

 

        String server = settings.getString("server", " ");

 

        String backup = settings.getString("backup", " ");

 

        Log.d("SharedPreferences", "server:" + server + ", backup:" + backup);

 

        Log.d("SharedPreferences", "获取数据成功");

 

        Uri server_uri = Uri.parse(server);

 

        Uri backup_uri = Uri.parse(backup);

 

        Intent intent1 = new Intent(Intent.ACTION_VIEW, server_uri);

 

        Intent intent2 = new Intent(Intent.ACTION_VIEW, backup_uri);

 

        startActivity(intent1);

 

        startActivity(intent2);

 

    }

 

 

 

你可能感兴趣的:(Preference)