标签:android sharedpreferences 存储
SharedPreferences是Android提供用来存储一些简单的配置信息的一种机制,例如,一些默认欢迎语、登录的用户名和密码等。其以键值对的方式存储,使得我们可以很方便的读取和存入,下面看一个演示的例子。<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" > <TableRow> <TextView android:gravity="right" android:textStyle="bold" android:padding="3dip" android:text="用户名称:" /> <EditText android:id="@+id/username" android:inputType="text" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow> <TableRow> <TextView android:gravity="right" android:textStyle="bold" android:padding="3dip" android:text="用户密码:" /> <EditText android:id="@+id/password" android:inputType="textPassword" android:padding="3dip" /> </TableRow> <TableRow android:gravity="right"> <Button android:id="@+id/cancel" android:text="取消" /> <Button android:id="@+id/login" android:text="登录" /> </TableRow> </TableLayout>
package com.rainsong.sharedpreferencesdemo; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final String PREFERENCES_NAME = "sharedpreferencesdemo"; private static final String KEY_USERNAME = "username"; private static final String KEY_PASSWORD = "password"; Button btn_login; Button btn_cancel; EditText et_username; EditText et_password; OnClickListener listener_login; OnClickListener listener_cancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText) findViewById(R.id.username); et_password = (EditText) findViewById(R.id.password); listener_login = new OnClickListener() { public void onClick(View v) { String username; String password; username = et_username.getText().toString(); if (username.length() < 1) { Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_LONG).show(); return; } password = et_password.getText().toString(); if (password.length() < 1) { Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_LONG).show(); return; } SharedPreferences pref = getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor edit = pref.edit(); edit.putString(KEY_USERNAME, username); edit.putString(KEY_PASSWORD, password); edit.commit(); Toast.makeText(MainActivity.this, "用户名称:" + username + ", 用户密码:" + password,Toast.LENGTH_SHORT).show(); } }; btn_login = (Button)findViewById(R.id.login); btn_login.setOnClickListener(listener_login); listener_cancel = new OnClickListener() { public void onClick(View v) { SharedPreferences pref = getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); Editor edit = pref.edit(); edit.clear(); edit.commit(); et_username.setText(""); et_password.setText(""); } }; btn_cancel = (Button)findViewById(R.id.cancel); btn_cancel.setOnClickListener(listener_cancel); SharedPreferences pref = getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND); String username; String password; username = pref.getString(KEY_USERNAME, ""); password = pref.getString(KEY_PASSWORD, ""); et_username.setText(username); et_password.setText(password); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
通过上述代码可以看到,在onCreate中使用findViewById得到两个EditText后,使用getSharedPreferences()方法取得SharedPreferences对象,然后使用getString()方法取得其中保存的值,最后使用setText()方法将其值设置为两个EditText的值。
在单击“登录”按钮时,首先使用getSharedPreferences()方法取得SharedPreferences对象;然后调用edit()方法使其处于可编辑状态,并使用putString()方法将两个EditText中的值保存起来;最后使用commit()方法提交即可保存。
Set a String value in the preferences editor, to be written back once commit() or apply() are called.
标签:android sharedpreferences 存储
原文地址:http://blog.csdn.net/hantangsongming/article/details/42079899