Android基礎--索引值對儲存(SharedPreferences)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

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