SharedPreferences實現記住密碼功能,js實現記住密碼功能

來源:互聯網
上載者:User

SharedPreferences實現記住密碼功能,js實現記住密碼功能
SharedPerferences 簡單介紹

  • 用於儲存簡單的索引值對資料;
  • 它將資料放在 /data/data/<package name>/shared_prefs目錄下,用xml檔案儲存MAP索引值對;
SharedPerferences 使用步驟將資料存放區到SharedPerferences中:

  1.先要得到SharedPerference對象:(三種方法)

      1).使用Context類中的 getSharedPreferences() 方法,它接收兩個參數,第一個參數為檔案名稱,第二個參數為操作模式。

       操作模式MODE_PRAVITE :只有當前程式才能對這個檔案進行讀寫。MODE_MULTI_PROCESS :多個進程中對同一個檔案進行讀寫。

       如:

SharedPreferences spf = getSharedPreferences("data",Context.MODE_PRIVATE);

 

      2).使用Activity類中的 getPreferences() 方法,它只接收一個參數--操作模式,並且會將當前活動的類名作為檔案名稱。

       如:

SharedPreferences spf = getPreferences(MODE_PRIVATE);

 

      3).使用PreferenceManager類中的 getDefaultSharedPreferences() 方法,它接收一個Context參數,並且用包名作為首碼來命名檔案。

       如:

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

 

  2.再得到SharedPreferences.Editor對象:

      使用SharedPreferences對象的 edit() 方法。

SharedPreference.Editor editor = spf.edit();

 

  3.開始添加資料:

      以索引值對的方式寫入資料。

editor.putInt("age",22);editor.putString("name","Visen");editor.putBoolean("singleDog",false)

 

  4.提交操作:

editor.commit();

 

從SharedPerferences中讀取資料:

  提供了一系列的get方法進行資料的讀取。如:

String name = editor.getString("name"," ");

 

  如果鍵所對應的值不存在,則填入設定的預設值。

 

 

簡單的儲存密碼功能

login.xml 登入布局頁面

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/black"    android:stretchColumns="1">    <LinearLayout        android:layout_height="wrap_content"        android:background="@color/black"        android:orientation="vertical">        <ImageView            android:layout_width="match_parent"            android:layout_height="240dp"            android:src="@drawable/image1"/>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@string/title"            android:textSize="40sp"            android:textColor="@color/red"            android:gravity="center"            android:background="@color/cyan"/>    </LinearLayout>   <TableRow       android:layout_marginTop="30dp">       <TextView           android:layout_height="wrap_content"           android:text="@string/account"           android:textSize="30sp"           android:textColor="@color/white"/>       <EditText           android:id="@+id/account"           android:layout_height="wrap_content"           android:inputType="text"           android:textSize="20sp"           android:textColor="@color/red"           android:gravity="center"           android:singleLine="true"/>   </TableRow>    <TableRow>        <TextView            android:layout_height="wrap_content"            android:text="@string/password"            android:textSize="30sp"            android:textColor="@color/white"/>        <EditText            android:id="@+id/passWord"            android:layout_height="wrap_content"            android:inputType="textPassword"            android:textSize="20sp"            android:textColor="@color/red"            android:gravity="center" />    </TableRow>    <TableLayout        android:layout_height="wrap_content"        android:stretchColumns="0">        <TableRow>            <CheckBox                android:id="@+id/saveSelect"                android:background="@color/red"                android:layout_gravity="end"/>            <TextView                android:layout_height="wrap_content"                android:text="@string/saveSelect"                android:textSize="20sp"                android:textColor="@color/white"                android:gravity="center"                android:layout_gravity="bottom"/>        </TableRow>        <TableRow>            <Button                android:layout_height="wrap_content"                android:id="@+id/login"                android:gravity="center"                android:layout_span="2"                android:text="@string/login"                android:textSize="25sp"                android:textColor="@color/red"                android:background="@drawable/black_bt"/>        </TableRow>    </TableLayout></TableLayout>

 

Login.java

public class Login extends AppCompatActivity {    private SharedPreferences spf;    private SharedPreferences.Editor spfe;    private int num = 0;    private EditText account = null;    private EditText passWord = null;    private CheckBox saveSelect = null;    private Button login = null ;    @Override    protected void onCreate(Bundle saveInstanceState){        //載入布局        super.onCreate(saveInstanceState);        setContentView(R.layout.login);        //初始化控制項        account = (EditText)findViewById(R.id.account);        passWord = (EditText)findViewById(R.id.passWord);        saveSelect = (CheckBox)findViewById(R.id.saveSelect);        login = (Button)findViewById(R.id.login);        //使用Context的getSharedPreferences(String name,int mode)方法得到SharedPreferences對象;        spf = getSharedPreferences("data", Context.MODE_PRIVATE);        //使用SharedPreferences對象的edit()方法得到 SharedPreferences.Editor 的對象;        spfe = spf.edit();        //複選框是否被選中,若為選中狀態,則儲存過賬戶,要恢複資料        if(spf.getBoolean("isSelect",false)){//選中標誌,預設值為false            String acc = spf.getString("account","");            String pas = spf.getString("passWord","");            account.setText(acc);            passWord.setText(pas);            saveSelect.setChecked(true);        }        //設定登入按鈕監聽事件        login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //確認帳號密碼                if(account.getText().toString().equals("visen") && passWord.getText().toString().equals("dsy402645063!")){                    //複選框是否被勾選,若被勾選,則需要儲存賬戶後登入;否則直接登入且不儲存賬戶                    if(saveSelect.isChecked()){                        saveDate();                    }else {                        spfe.clear();                        spfe.commit();                    }                                        //頁面跳轉                    Intent intent = new Intent(Login.this,MainActivity.class);                    startActivity(intent);                    finish();                }else {//賬戶或密碼錯誤                    Toast.makeText(Login.this, "account or password is invalid", Toast.LENGTH_SHORT).show();                }            }        });    }    public void saveDate(){        //讀取EditText中的內容        String acc = account.getText().toString();        String pas = passWord.getText().toString();        //儲存資料        spfe.putString("account",acc);        spfe.putString("passWord",pas);        spfe.putBoolean("isSelect",true);        //提交        spfe.commit();    }        @Override    public void onBackPressed(){        num++;        if(num == 2){            super.onBackPressed();        }else{            Toast.makeText(Login.this, "再按一次退出程式", Toast.LENGTH_SHORT).show();        }    }}

 

聯繫我們

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