標籤:des android style blog http io ar color os
SharedPreferences用於將索引值對形式的資料存放區到當前應用專屬的儲存空間中
package com.itheima.sharedpreferences;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.text.TextUtils;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText et_username; private EditText et_password; private CheckBox cb_isAutoLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_username = (EditText) findViewById(R.id.et_username); et_password = (EditText) findViewById(R.id.et_password); cb_isAutoLogin = (CheckBox) findViewById(R.id.isAutoLogin); readAccount(); } //如果之前使用者將資訊儲存到手機上,需要對使用者資訊進行回顯 private void readAccount() { SharedPreferences sPreferences = getSharedPreferences("userInfo", MODE_PRIVATE); String username =sPreferences.getString("username", ""); String password = sPreferences.getString("password", ""); et_username.setText(username); et_password.setText(password); } // 登入 ,將使用者名稱和密碼儲存到內部儲存空間 public void login(View v) { String username = et_username.getText().toString(); String password = et_password.getText().toString(); if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ Toast.makeText(this, "使用者名稱或密碼不可為空", Toast.LENGTH_SHORT).show(); return; } if (cb_isAutoLogin.isChecked()) { //檔案名稱不用寫副檔名,會自動添加xml副檔名 /* * name Desired preferences file. * If a preferences file by this name does not exist, it will be created * when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()). */ SharedPreferences sPreferences = getSharedPreferences("userInfo", MODE_PRIVATE); Editor editor = sPreferences.edit(); editor.putString("username", username); editor.putString("password", password); editor.commit(); } Toast.makeText(this, "登入成功", 0).show(); } }
檔案儲存體結構如:
布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/_username" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/_password" android:inputType="textPassword"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/isAutoLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/_autologin" android:layout_centerVertical="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/_login" android:onClick="login"/> </RelativeLayout></LinearLayout>
Android基礎--索引值對儲存(SharedPreferences)