您的位置:首页 > 其它

记住密码

2015-07-04 15:10 288 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/nimaba7474/article/details/46755437
  1. <RelativeLayout  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="wrap_content" >  
  4.         <ImageButton   
  5.             android:id="@+id/img_btn"  
  6.             android:layout_width="wrap_content"  
  7.             android:layout_height="wrap_content"  
  8.             android:layout_alignParentRight="true"  
  9.             android:background="@drawable/quit"/>  
  10.   
  11.         <TextView  
  12.             android:id="@+id/tv_zh"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="35dip"  
  15.             android:layout_marginLeft="12dip"  
  16.             android:layout_marginTop="10dip"  
  17.             android:gravity="bottom"  
  18.             android:text="帐号:"  
  19.             android:textColor="#000000"  
  20.             android:textSize="18sp" />  
  21.   
  22.         <EditText  
  23.             android:id="@+id/et_zh"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="40dip"  
  26.             android:layout_below="@id/tv_zh"  
  27.             android:layout_marginLeft="12dip"  
  28.             android:layout_marginRight="10dip" />  
  29.   
  30.         <TextView  
  31.             android:id="@+id/tv_mima"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="35dip"  
  34.             android:layout_below="@id/et_zh"  
  35.             android:layout_marginLeft="12dip"  
  36.             android:layout_marginTop="10dip"  
  37.             android:gravity="bottom"  
  38.             android:text="密码:"  
  39.             android:textColor="#000000"  
  40.             android:textSize="18sp" />  
  41.   
  42.         <EditText  
  43.             android:id="@+id/et_mima"  
  44.             android:layout_width="fill_parent"  
  45.             android:layout_height="40dip"  
  46.             android:layout_below="@id/tv_mima"  
  47.             android:layout_marginLeft="12dip"  
  48.             android:layout_marginRight="10dip"  
  49.             android:maxLines="200"  
  50.             android:password="true"  
  51.             android:scrollHorizontally="true" />  
  52.   
  53.         <CheckBox  
  54.             android:id="@+id/cb_mima"  
  55.             android:layout_width="wrap_content"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_below="@id/et_mima"  
  58.             android:layout_marginLeft="12dip"  
  59.             android:text="记住密码"  
  60.             android:textColor="#000000" />  
  61.   
  62.         <CheckBox  
  63.             android:id="@+id/cb_auto"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:layout_below="@id/cb_mima"  
  67.             android:layout_marginLeft="12dip"  
  68.             android:text="自动登录"  
  69.             android:textColor="#000000" />  
  70.         <Button  
  71.             android:id="@+id/btn_login"  
  72.             android:layout_width="80dip"  
  73.             android:layout_height="40dip"  
  74.             android:layout_below="@id/et_mima"  
  75.             android:layout_alignParentRight="true"  
  76.             android:layout_alignTop="@id/cb_auto"  
  77.             android:layout_marginRight="10dip"  
  78.             android:gravity="center"  
  79.             android:text="登录"  
  80.             android:textColor="#000000"  
  81.             android:textSize="18sp"/>  
  82.   
  83.           
  84.     </RelativeLayout>  
  85.       

2.主要代码

[html] view plaincopy
  1.     //判断记住密码多选框的状态  
  2.       if(sp.getBoolean("ISCHECK", false))  
  3.         {  
  4.           //设置默认是记录密码状态  
  5.           rem_pw.setChecked(true);  
  6.           userName.setText(sp.getString("USER_NAME", ""));  
  7.           password.setText(sp.getString("PASSWORD", ""));  
  8.           //判断自动登陆多选框状态  
  9.           if(sp.getBoolean("AUTO_ISCHECK", false))  
  10.           {  
  11.                  //设置默认是自动登录状态  
  12.                  auto_login.setChecked(true);  
  13.                 //跳转界面  
  14.                 Intent intent = new Intent(LoginActivity.this,LogoActivity.class);  
  15.                 LoginActivity.this.startActivity(intent);  
  16.           }  
  17.         }  
  18.           
  19.         // 登录监听事件  现在默认为用户名为:admin 密码:123456  
  20.         btn_login.setOnClickListener(new OnClickListener() {  
  21.   
  22.             public void onClick(View v) {  
  23.                 userNameValue = userName.getText().toString();  
  24.                 passwordValue = password.getText().toString();  
  25.                   
  26.                 if(userNameValue.equals("admin")&&passwordValue.equals("123456"))  
  27.                 {  
  28.                     Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();  
  29.                     //登录成功和记住密码框为选中状态才保存用户信息  
  30.                     if(rem_pw.isChecked())  
  31.                     {  
  32.                      //记住用户名、密码、  
  33.                       Editor editor = sp.edit();  
  34.                       editor.putString("USER_NAME", userNameValue);  
  35.                       editor.putString("PASSWORD",passwordValue);  
  36.                       editor.commit();  
  37.                     }  
  38.                     //跳转界面  
  39.                     Intent intent = new Intent(LoginActivity.this,LogoActivity.class);  
  40.                     LoginActivity.this.startActivity(intent);  
  41.                     //finish();  
  42.                       
  43.                 }else{  
  44.                       
  45.                     Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();  
  46.                 }  
  47.                   
  48.             }  
  49.         });  
  50.   
  51.         //监听记住密码多选框按钮事件  
  52.         rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  53.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
  54.                 if (rem_pw.isChecked()) {  
  55.                       
  56.                     System.out.println("记住密码已选中");  
  57.                     sp.edit().putBoolean("ISCHECK", true).commit();  
  58.                       
  59.                 }else {  
  60.                       
  61.                     System.out.println("记住密码没有选中");  
  62.                     sp.edit().putBoolean("ISCHECK", false).commit();  
  63.                       
  64.                 }  
  65.   
  66.             }  
  67.         });  
  68.           
  69.         //监听自动登录多选框事件  
  70.         auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  71.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
  72.                 if (auto_login.isChecked()) {  
  73.                     System.out.println("自动登录已选中");  
  74.                     sp.edit().putBoolean("AUTO_ISCHECK", true).commit();  
  75.   
  76.                 } else {  
  77.                     System.out.println("自动登录没有选中");  
  78.                     sp.edit().putBoolean("AUTO_ISCHECK", false).commit();  
  79.                 }  
  80.             }  
  81.         });  
  82.           
  83.         btnQuit.setOnClickListener(new OnClickListener() {  
  84.               
  85.             @Override  
  86.             public void onClick(View v) {  
  87.                 finish();  
  88.             }  
  89.         });  
  90.   
  91.     }  
  92. }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: