Android開發學習---sharedpreference的使用

在前面文章中,爲了使數據回顯,使用的技術思路是,首先,將數據持久化寫到ROM或者SDCard中,其中name和password以":"分隔;而後,將數據記取出來,再用split方法將數據切割,分割後的數據再set到輸入框中.整個過程可謂是至關麻煩,一不當心就會報空指針的錯誤,並且很容易忘記寫參數.下面將學習一個叫SharedPreference東西.SharedPreference主要用來保存參數.java

1.效果:

 

 

2.實現代碼

package com.amos.sharedpreference;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
    String tag = "MainActivity";
    EditText et_name;// 用戶名
    EditText et_password;// 密碼
    Button bt_login;// 登陸按鈕
    CheckBox cb_password;// 單選框

    // 用來保存參數的接口
 SharedPreferences sharedPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化
        et_name = (EditText) this.findViewById(R.id.et_name);
        et_password = (EditText) this.findViewById(R.id.et_password);
        bt_login = (Button) this.findViewById(R.id.bt_login);
        cb_password = (CheckBox) this.findViewById(R.id.cb_password);

        // 初始化參數配置
        sharedPreference=this.getSharedPreferences("spconfig", MODE_PRIVATE); if(sharedPreference.getBoolean("issetup", false)){
            et_name.setText(sharedPreference.getString("name", ""));
            et_password.setText(sharedPreference.getString("password", ""));
        }
        
        
        // 註冊監聽事件
        bt_login.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_login:
            String name = et_name.getText().toString().trim();
            String password = et_password.getText().toString().trim();
            System.out.println("name:"+name);
            System.out.println("password:"+password);
            // 定義用來保存參數的接口
            Editor edit = sharedPreference.edit(); edit.putString("name", name); edit.putString("password", password); edit.putBoolean("issetup", true); //清空編輯器
            //edit.clear();
            
            // 當有兩個編輯器(Editor)進行編輯同一個sharedPreference時,最後一個提交的將會生效
 edit.commit(); default:
            break;
        }

    }

}

 

sharedpreference的使用是至關方便,這裏一樣會將用戶名密碼保存下來,但這裏不一樣點在於,經過sharedPreference會生成規整的xml的文件,這裏主要用到的是Edit類中的方法去put值到相應的位置.通常狀況下其它配置也是會保存到sharedPreference中,如setting:linux

 

 3.補充

1).使用openFileInput('...')讀取數據.

前面文章中將密碼寫到rom文件中主要採用的是openFileOutput('XXX/xxx').android

這裏一樣能夠相似的經過openFileInput('xx/xxx')進行讀取文件.比較簡單,這裏再也不舉例.app

2).使用命令生成sdcard

t-bundle-linux-x86_64-20131030/sdk/tools$ ./mksdcard 20
mksdcard: create a blank FAT32 image to be used with the Android emulator
usage: mksdcard [-l label] <size> <file>

  if <size> is a simple integer, it specifies a size in bytes
  if <size> is an integer followed by 'K', it specifies a size in KiB
  if <size> is an integer followed by 'M', it specifies a size in MiB
  if <size> is an integer followed by 'G', it specifies a size in GiB

Minimum size is 9M. The Android emulator cannot use smaller images.
Maximum size is 1099511627264 bytes, 1073741823K, 1048575M or 1023G
amosli@amosli-pc:/media/f91a4cca-0b96-4c30-b140-7918a196de3e/amosli/java/soft/adt-bundle-linux-x86_64-20131030/sdk/tools$ 

 3).更新ADT(android development tools )eclipse

方法一:將 https://dl-ssl.google.com/android/eclipse/ 連接加入到, Help > Install New Software > add url編輯器

方法二:直接下載zip包ADT-22.6.3.zip (https://dl.google.com/android/ADT-22.6.3.zip),不用解壓.步驟與add url相似,官方介紹以下.ide

  1. Download the ADT Plugin zip file (do not unpack it):
    Package Size MD5 Checksum
    ADT-22.6.3.zip 14590813 bytes 3982259fd2cc81e53bbbe05dcd6529a7
  2. Start Eclipse, then select Help > Install New Software.
  3. Click Add, in the top-right corner.
  4. In the Add Repository dialog, click Archive.
  5. Select the downloaded ADT-22.6.3.zip file and click OK.
  6. Enter "ADT Plugin" for the name and click OK.
  7. In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
  8. In the next window, you'll see a list of the tools to be downloaded. Click Next.
  9. Read and accept the license agreements, then click Finish.

也能夠直接從百度網盤中下載(推薦):http://pan.baidu.com/s/1i3rAc2D學習

相關文章
相關標籤/搜索