MiniTwitter记住密码等功能实现
一、SharedPreferences的用法:(相关实现功能的只是了解)
由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,用法见下面代码:
Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例
name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。
mode:是指定读写方式,其值有三种,分别为:
Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写
Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写
Context.MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序读写。
二、实现功能的分析:
如果设置记住密码,自动登录等复选框,也是
将用户输入的数据进行保存,保存在xml文件中,再从中进行读取,如果正确,直接进入下一个成功后的界面,当用户下一次进入时,首先判断输入的文本在
xml中有没有记录,如果有记录,就直接从xml文件中读取,实现了记住密码的功能。
三、具体实现如下:
1、MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
    
    private EditText userName, password;
    private CheckBox rem_pw, auto_login;
    private Button btn_login;
    private ImageButton btnQuit;
    private String userNameValue,passwordValue;
    private SharedPreferences sp;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //去除标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        
        //获得实例对象
        sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
        userName = (EditText) findViewById(R.id.et_zh);
        password = (EditText) findViewById(R.id.et_mima);
        rem_pw = (CheckBox) findViewById(R.id.cb_mima);
        auto_login = (CheckBox) findViewById(R.id.cb_auto);
        btn_login = (Button) findViewById(R.id.btn_login);
        btnQuit = (ImageButton)findViewById(R.id.img_btn);
        
        
        //判断记住密码多选框的状态
      if(sp.getBoolean("ISCHECK", false))
        {
          //设置默认是记录密码状态
          rem_pw.setChecked(true);
             userName.setText(sp.getString("USER_NAME", ""));
             password.setText(sp.getString("PASSWORD", ""));
             //判断自动登陆多选框状态
             if(sp.getBoolean("AUTO_ISCHECK", false))
             {
                    //设置默认是自动登录状态
                    auto_login.setChecked(true);
                   //跳转界面
                Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
                LoginActivity.this.startActivity(intent);
             }
        }
        
        // 登录监听事件  现在默认为用户名为:xuyinghui 密码:123
        btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
                userNameValue = userName.getText().toString();
                passwordValue = password.getText().toString();
                
                if(userNameValue.equals("xuyinghui")&&passwordValue.equals("123"))
                {
                    Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
                    //登录成功和记住密码框为选中状态才保存用户信息
                    if(rem_pw.isChecked())
                    {
                     //记住用户名、密码、
                      Editor editor = sp.edit();
                      editor.putString("USER_NAME", userNameValue);
                      editor.putString("PASSWORD",passwordValue);
                      editor.commit();
                    }
                    //跳转界面
                    Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
                    LoginActivity.this.startActivity(intent);
                    //finish();
                    
                }else{
                    
                    Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
                }
                
            }
        });
//监听记住密码多选框按钮事件
        rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (rem_pw.isChecked()) {
                    
                    System.out.println("记住密码已选中");
                    sp.edit().putBoolean("ISCHECK", true).commit();
                    
                }else {
                    
                    System.out.println("记住密码没有选中");
                    sp.edit().putBoolean("ISCHECK", false).commit();
                    
                }
}
        });
        
        //监听自动登录多选框事件
        auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (auto_login.isChecked()) {
                    System.out.println("自动登录已选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
                    System.out.println("自动登录没有选中");
                    sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
                }
            }
        });
        
        btnQuit.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                finish();
            }
        });
}
}
2、页面布局:
先建立一个activity_main.xml 。代码如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/loginbg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<include layout="@layout/login_top"/>
<include layout="@layout/login_bottom"/>"
</LinearLayout>
3、页面布局login.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvRegist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="18dp"
android:text="@string/tvRegister"
android:autoLink="all"
android:textColorLink="#FF0066CC" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:src="@drawable/panda" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:src="@drawable/icon" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/btnbg_roundcorner" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" > 
 
    <TextView 
        android:id="@+id/tvUsername" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:text="@string/tvName" 
        android:textAppearance="?android:attr/textAppearanceMedium" /> 
 
    <EditText 
        android:id="@+id/etUsername" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/tvUsername" 
        android:layout_below="@+id/tvUsername" 
        android:background="@android:drawable/edit_text" 
        android:ems="10" > 
 
        <requestFocus /> 
    </EditText> 
 
    <TextView 
        android:id="@+id/tvPassword" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/etUsername" 
        android:layout_below="@+id/etUsername" 
        android:text="@string/tvPassword" 
        android:textAppearance="?android:attr/textAppearanceMedium" /> 
 
    <EditText 
        android:id="@+id/etPassword" 
       android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/tvPassword" 
        android:layout_below="@+id/tvPassword" 
        android:layout_marginTop="16dp" 
        android:background="@android:drawable/edit_text" 
        android:ems="10" 
        android:inputType="textPassword" /> 
 
    <Button 
        android:id="@+id/btnLogin" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignRight="@+id/etPassword" 
        android:layout_below="@+id/etPassword" 
       android:layout_marginTop="20dp" 
        android:background="#FF72CAE1" 
        android:text="@string/btnLogin" /> 
 
    <CheckBox 
        android:id="@+id/cbRememberPass" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/etPassword" 
        android:layout_alignTop="@+id/btnLogin" 
        android:text="记住密码" /> 
 
</RelativeLayout> 
MiniTwitter记住密码等功能实现的更多相关文章
- (四)SpringMVC之使用cookie实现记住密码的功能
		
注意:因为实现记住密码的功能需要用到json,所以需要加上这条语句: <script type="text/javascript" src="scripts/jqu ...
 - Cookie实现记住密码的功能
		
一.什么是Cookie cookie是一种WEB服务器通过浏览器在访问者的硬盘上存储信息的手段.Cookie的目的就是为用户带来方便,为网站带来增值.虽然有着许多误传,事实上Cookie并不会造成严重 ...
 - JavaWeb学习----Cookie实现记住密码的功能
		
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
 - 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
		
function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd ...
 - 登陆一个系统时,前端js实现的验证,记住密码等功能
		
记住密码部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
 - 014. asp.net实现记住密码的功能
		
protected void Button1_Click(object sender, EventArgs e) { if (txtname.Text.Trim().Equals("mr&q ...
 - 超简单的localStorage实现记住密码的功能
		
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...
 - android: SharedPreferences实现记住密码功能
		
既然是实现记住密码的功能,那么我们就不需要从头去写了,因为在上一章中的最佳实 践部分已经编写过一个登录界面了,有可以重用的代码为什么不用呢?那就首先打开 BroadcastBestPractice 项 ...
 - 记住密码"功能的正确设计
		
Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能.下面的文章告诉大家这个功能可能并没有你所想像的那么简单,这是一个关 ...
 
随机推荐
- 空间点绕轴旋转公式&程序(C++)
			
关键词:空间旋转.旋转轴 用途:相机位姿估计.无人机位姿估计.3D游戏.3D建模 文章类型:概念.公式总结(本文不带推倒过程,若想了解公式是如何推出来的请搜索文献),C++函数展示 @Author:V ...
 - 谈谈Memcached与Redis
			
1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...
 - Javascript 字符串常用操作方法
			
1.字符串转换 /* 你可以将一个数字,布尔值,或一个字符串对象转换为字符串 */ var num= 18; var str1 = num.toString(); //'18' var str2 = ...
 - java归并排序,单线程vs多线程
			
一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...
 - 关于String的equals问题和StringBuilder问题
			
今天弄了一下String和StringBuilder,遇到了一些问题. 老规矩上代码图片: 发现stringBuilder的equals方法有问题,看了一下源码,发现stringbuilder没有重写 ...
 - 贪心算法(Greedy Algorithm)
			
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
 - LoadRunner脚本参数化设置
			
LoadRunner,是一种预测系统行为和性能的负载测试工具,其中VUG(Virtual User Generator)主要用于虚拟用户生成和脚本编辑.为了实现单用户多次迭代执行脚本,VUG提供了强大 ...
 - 磁盘文件系统Fat、Fat32、NTFS、exFAT的优缺点
			
我们在Windows系统里格式化磁盘的时候,文件系统的选项里可以看到有“FAT”.“FAT32”.“NTFS”等选项,在对U盘或其他移动存储设备 格式化的时候还会出现“exFAT”选项,那么这四种磁盘 ...
 - ubuntu 14.04 64位安装iNodeClient
			
ubuntu 14.04 64位安装iNodeClieng(华三校园客服端) http://pan.baidu.com/s/12dpxk ubuntu 14.04 64bit Install H3C ...
 - xtrapivotcontrol  控件用法及相关属性
			
XtraPivotControl使用指南:可参照以下的文档: 转载自:http://wenku.baidu.com/view/d7886b20aaea998fcc220e53.html 上面链接中对x ...