Android——登录界面、SharedPreferences实现记住密码等账户信息

先看下效果图:


该界面的布局文件为:

[java]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:stretchColumns="1"  
  7.     android:layout_marginLeft="10dp"  
  8.     android:layout_marginRight="10dp"  
  9.     android:layout_marginTop="10dp"  
  10.     tools:context="xjtu.com.rememberpwd.MainActivity">  
  11.   
  12.     <TableRow>  
  13.         <TextView android:text="账号:"  
  14.             android:paddingLeft="30dp"/>  
  15.         <EditText android:id="@+id/username"  
  16.             android:background="@drawable/et_shape"/>  
  17.     </TableRow>  
  18.   
  19.     <TableRow android:layout_marginTop="10dp">  
  20.   
  21.         <TextView android:text="密码:"  
  22.             android:paddingLeft="30dp"/>  
  23.         <EditText android:id="@+id/password"  
  24.             android:background="@drawable/et_shape"  
  25.             android:inputType="textPassword"/>  
  26.     </TableRow>  
  27.   
  28.     <TableRow android:layout_marginTop="10dp">  
  29.   
  30.         <CheckBox android:id="@+id/chk"  
  31.             android:text="记住密码"/>  
  32.         <Button android:id="@+id/btn_login"  
  33.             android:background="@drawable/btn_shape"  
  34.             android:text="登录"/>  
  35.   
  36.     </TableRow>  
  37.   
  38. </TableLayout>  


业务逻辑代码为:


[java]  view plain  copy
 print ?
  1. import android.content.Intent;  
  2. import android.content.SharedPreferences;  
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.CheckBox;  
  8. import android.widget.EditText;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     private Button btn_login;  
  13.     private EditText username,password;  
  14.     private CheckBox chk;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         initView();  
  20.         restoreInfo();  
  21.     }  
  22.   
  23.     private void initView(){  
  24.         btn_login=(Button)findViewById(R.id.btn_login);  
  25.         username=(EditText)findViewById(R.id.username);  
  26.         password=(EditText)findViewById(R.id.password);  
  27.         chk=(CheckBox)findViewById(R.id.chk);  
  28.   
  29.         btn_login.setOnClickListener(new View.OnClickListener() {  
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 if (chk.isChecked()){  
  33.                     String usr=username.getText().toString();  
  34.                     String pwd=password.getText().toString();  
  35.                     memInfo(usr,pwd);  
  36.                 }else{  
  37.                     SharedPreferences.Editor et=getSharedPreferences("data",0).edit();  
  38.                     et.clear();  
  39.                     et.commit();  
  40.                 }  
  41.                 Intent intent=new Intent(MainActivity.this,LoginSuccess.class);  
  42.                 startActivity(intent);  
  43.             }  
  44.         });  
  45.   
  46.   
  47.     }  
  48.   
  49.     private void memInfo(String usr,String pwd){  
  50.         SharedPreferences.Editor editor=getSharedPreferences("data",0).edit();  
  51.         editor.putString("username",usr);  
  52.         editor.putString("password",pwd);  
  53.         editor.commit();  
  54.     }  
  55.   
  56.     private void restoreInfo(){  
  57.         SharedPreferences sp=getSharedPreferences("data",0);  
  58.         username.setText(sp.getString("username",""));  
  59.         password.setText(sp.getString("password",""));  
  60.     }  
  61.   
  62. }  
主要思想:在输入登录信息后,点击“登录”前,如果勾选记住密码,此时,点击“登录”会调用memInfo方法把信息用 SharedPrefenerces 技术存入文件名为data的文件中。软件每次启动时会在onCreate方法中把保存的信息恢复并自动回填到到相应的EditText中(第一次启动没有,因为还没有创建data文件)。如果不勾选,就不会记住密码,就算之前记住了,也会清空,因为data文件被清空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值