溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能

發(fā)布時(shí)間:2020-09-08 03:33:55 來(lái)源:腳本之家 閱讀:187 作者:ly1012053441 欄目:移動(dòng)開(kāi)發(fā)

Android存儲(chǔ)方式有很多種,在這里所用的存儲(chǔ)方式是SharedPreferrences, 其采用了Map數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)數(shù)據(jù),以鍵值的方式存儲(chǔ),可以簡(jiǎn)單的讀取與寫(xiě)入。所以比較適合我們今天做的這個(gè)項(xiàng)目。我們來(lái)看一下運(yùn)行圖:

Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能

一.布局界面

1.login_top.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/activity_horizontal_margin"
 android:background="@drawable/logintop_roundbg">
 <EditText
 android:id="@+id/etName"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:drawablePadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableLeft="@drawable/icon_user"
 android:hint="@string/etName">
 <requestFocus></requestFocus>
 </EditText>
 <EditText
 android:id="@+id/etPassword"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/etName"
 android:inputType="textPassword"
 android:ems="10"
 android:drawablePadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableLeft="@drawable/icon_pass"
 android:hint="@string/etpassword">
 <requestFocus></requestFocus>
 </EditText>
 <CheckBox
 android:id="@+id/cbremenber"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/etPassword"
 android:text="@string/cbpass"/>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/cbremenber">
 <Button
  android:id="@+id/btnlogin"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnlogin"
  android:onClick="login"/>
 <Button
  android:id="@+id/btnRegister"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnRegister"
  android:layout_marginLeft="10dp"/>
 </LinearLayout>
</RelativeLayout>

2.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/loginbg"
 tools:context="cn.edu.bzu.logindemo.MainActivity">
 <include layout="@layout/login_top"></include>
 <ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/deer"
 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true" />
</RelativeLayout>

3.activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_welcome"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="cn.edu.bzu.logindemo.WelcomeActivity">
 <TextView
 android:id="@+id/tvwelcome"
 android:text="Welcome you"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="200dp"
 android:textSize="40sp"
  />
</RelativeLayout>

二.MainActivity

public class MainActivity extends AppCompatActivity {
 private EditText etName;
 private EditText etPassword;
 private SharedPreferences sharedPreferences;
 private CheckBox cbremenber;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initViews();
 sharedPreferences=getSharedPreferences("remenberpassword", Context.MODE_PRIVATE);
 boolean isRemember=sharedPreferences.getBoolean("remenberpassword",false);
 if(isRemember) {
  String name = sharedPreferences.getString("name", "");
  String password = sharedPreferences.getString("password", "");
  etName.setText(name);
  etPassword.setText(password);
  cbremenber.setChecked(true);
 }
 }
 private void initViews() {
 etName=(EditText) findViewById(R.id.etName);
 etPassword=(EditText) findViewById(R.id.etPassword);
 cbremenber=(CheckBox)findViewById(R.id.cbremenber);
 }
 public void login(View view){
 String name=etName.getText().toString();
 String password=etPassword.getText().toString();
 if("admin".equals(name)&&"123456".equals(password)){
  SharedPreferences.Editor editor= sharedPreferences.edit();
  if(cbremenber.isChecked()){
  editor.putBoolean("remenberpassword",true);
  editor.putString("name",name);
  editor.putString("password",password);
  }else {
  editor.clear();
  }
  editor.commit();
  Intent intent=new Intent(this,WelcomeActivity.class);
  startActivity(intent);
  finish();
 }else {
  Toast.makeText(this,"賬號(hào)或密碼有誤",Toast.LENGTH_LONG).show();
 }
 }
}

三.WelcomeActivity

 public class WelcomeActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_welcome);
 }
}

以上所述是小編給大家介紹的Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI