SharedPreferences

SharedPreferences

SharedPreferences是什麼

  • SharedPreferences是一種輕量級的數據存儲方式,通常用來存儲一些簡單的配置消息。比如:用戶名密碼,是否是第一次登陸,是否顯示引導頁。

如何存儲數據

           //實例化SharedPreferences對象(第一步)
            SharedPreferences sharedPreferences = getSharedPreferences("username", MODE_PRIVATE);
            //實例化SharedPreferences.Editor對象(第二步)
            SharedPreferences.Editor editor = sharedPreferences.edit();
            //用putString的方法保存數據
            editor.putString("username","張三");
            editor.putString("psw","asd");
            //提交當前數據 
            editor.commit();

如何讀取數據

//在讀取SharedPreferences數據前要實例化出一個SharedPreferences對象
        SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
        // 使用getString方法獲得value,注意第2個參數是value的默認值
        String name=sharedPreferences.getString("username","");
        String psw=sharedPreferences.getString("psw","");
        //用settext方法替換成文件中保存的內容
        neicun_name.setText(name);
        neicun_password.setText(psw);

記住密碼案例

  • 佈局代碼如下

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/neicun_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="30dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入你的名字"
            android:id="@+id/neicun_ed"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/neicun_tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密碼:"
            android:textSize="30dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入你的密碼"
            android:id="@+id/neicun_ed1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="記住密碼"
            android:id="@+id/neicun_rb"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自動登錄"
            android:id="@+id/neicun_rb1"
            />
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
              <Button
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:text="登錄"
                  android:id="@+id/neicun_btn"
                  />
               <Button
                   android:layout_width="0dp"
                   android:layout_weight="1"
                   android:layout_height="wrap_content"
                   android:text="註冊"
                   />
        </LinearLayout>
  • Activity代碼如下
   SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
         final int checked=sharedPreferences.getInt("checked",0);
         if (checked==1){
             String name=sharedPreferences.getString("username","");
             String psw=sharedPreferences.getString("psw","");
             neicun_name.setText(name);
             neicun_password.setText(psw);
             rd.setChecked(true);
         }else {
              rd.setChecked(false);
         }

         bt.setOnClickListener(new View.OnClickListener() {

             SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
              SharedPreferences.Editor editor=sharedPreferences.edit();
             public void onClick(View view) {
                    if (rd.isChecked()){
                        String name=neicun_name.getText().toString();
                        String psw=neicun_password.getText().toString();
                        editor.putString("username",name);
                        editor.putString("psw",psw);
                       editor.putInt("checked",1);

                    }else {

                        editor.putString("username","");
                        editor.putString("psw","");
                        editor.putInt("checked",0);
                    }
                 editor.commit();
             }
         });
           }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章