Android中SharePreferences的簡單實現

來源:互聯網
上載者:User

標籤:android   style   blog   class   c   code   

Android中提供SharePreferences這種輕量級的資料存放區模式,這種模式能夠儲存少量資料,並能為自身和其他應用提供資料介面。相對於其他資料存放區方式,SharePreferences更加輕量。以下是整個SharePreferences實現的代碼:

xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >    <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/editText" android:width="200dp"/>    <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:inputType="textPassword"            android:ems="10"            android:id="@+id/editText2"/>    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="儲存"            android:id="@+id/button"/>    <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="New Text"            android:id="@+id/textView"/>    <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="New Text"            android:id="@+id/textView2"/>    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示"            android:id="@+id/button2"/></LinearLayout>

 

java主檔案:

package com.example.sharepreferencesTest;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MyActivity extends Activity {    /**     * Called when the activity is first created.     */    private EditText user;    private EditText address;    private Button login;    private Button show;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        user = (EditText)findViewById(R.id.editText);        address = (EditText)findViewById(R.id.editText2);        login = (Button)findViewById(R.id.button);        login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //寫入SharedPreferences中                SharedPreferences user_ino = getSharedPreferences("user_ino", Context.MODE_PRIVATE);  //使用MODE_PRIVATE模式                SharedPreferences.Editor editor = user_ino.edit();                String ename = user.getText().toString();                String eaddress = address.getText().toString();                editor.putString("name",ename);                editor.putString("address", eaddress);                editor.commit();            }        });        show = (Button)findViewById(R.id.button2);        show.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //讀入SharedPreferences中的值                SharedPreferences user_ino2 = getSharedPreferences("user_ino", Context.MODE_PRIVATE);                String name1 = user_ino2.getString("name","");                String age1 = user_ino2.getString("address","");                //顯示出讀出的值                TextView tv1 = (TextView)findViewById(R.id.textView);                TextView tv2 = (TextView)findViewById(R.id.textView2);                tv1.setText(name1);                tv2.setText(age1);            }        });    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.