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

來源:互聯網
上載者:User

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:      3.補充1).使用openFileInput('...')讀取資料.前面文章中將密碼寫到rom檔案中主要採用的是openFileOutput('XXX/xxx'). 這裡同樣可以類似的通過openFileInput('xx/xxx')進行讀取檔案.比較簡單,這裡不再舉例. 2).使用命令產生sdcard複製代碼t-bundle-linux-x86_64-20131030/sdk/tools$ ./mksdcard 20mksdcard: create a blank FAT32 image to be used with the Android emulatorusage: 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 1023Gamosli@amosli-pc:/media/f91a4cca-0b96-4c30-b140-7918a196de3e/amosli/java/soft/adt-bundle-linux-x86_64-20131030/sdk/tools$ 複製代碼 3).更新ADT(android development tools ) 方法一:將 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類似,官方介紹如下. Download the ADT Plugin zip file (do not unpack it):Package Size MD5 ChecksumADT-22.6.3.zip 14590813 bytes 3982259fd2cc81e53bbbe05dcd6529a7Start Eclipse, then select Help > Install New Software.Click Add, in the top-right corner.In the Add Repository dialog, click Archive.Select the downloaded ADT-22.6.3.zip file and click OK.Enter "ADT Plugin" for the name and click OK.In the Available Software dialog, select the checkbox next to Developer Tools and click Next.In the next window, you'll see a list of the tools to be downloaded. Click Next.Read and accept the license agreements, then click Finish.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.