Android用户登录注册界面开发及用户信息管理案例代码详解

用户登录注册界面开发及用户信息管理案例详解

       刚开始接触Android编程,这算是我写的第一个简单工程,主要功能有:用户登录、注册、注销、修改密码、记住密码共5个基本操作,其内容涉及到以下几点:

       1:Button,TextView,TextEdit,ImageView,RatioButton等基本控件的使用;

       2:多个Activities之间的切换;

       3:使用Android本地数据库来管理用户信息。

       实现上述功能的主要参考资料:CSDN博客之星郭霖大牛写的《第一行代码—Android》,个人比较喜欢看纸质的资料,所以买了这本书,大牛的博客主页链接http://my.csdn.net/sinyu890807。其次是参考了网上的一个用户登录注册的程序,在此基础上按照自己的需求慢慢添加完善一些功能,并且将原来的单个avtivity换用多个activity实现,UI设计也更加漂亮美观。

        先上几个图【我在android6.0.1的真机上调试的】:

        下面是用户登录和用户注册界面。

       

       下面是用户密码修改和个人中心界面。

       

一、工程代码

       整个工程是在Android Studio 2.1.1环境下实现的,源码下载链接在末尾给出,下面是工程目录:
         
        这里先贴出各个布局文件.xml,各个类内容和各个activity代码:

1:几个界面布局文件.xml

        login.xml【登录界面】
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.foolishfan.user_v10.Login">  
  11.     <RelativeLayout  
  12.         android:id="@+id/login_view"  
  13.         android:layout_width="400dp"  
  14.         android:layout_height="800dp"  
  15.         android:layout_centerInParent="true"  
  16.          >  
  17.   
  18.         <Button  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="注册"  
  22.             android:id="@+id/login_btn_register"  
  23.             android:onClick="resetpwd"  
  24.             android:textColor="#ffffff"  
  25.             android:background="#e52525"  
  26.             android:textSize="20dp"  
  27.             android:layout_below="@+id/login_btn_login"  
  28.             android:layout_alignParentLeft="true"  
  29.             android:layout_alignParentStart="true"  
  30.             android:layout_marginTop="10dp" />  
  31.   
  32.         <Button  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="登录"  
  36.             android:id="@+id/login_btn_login"  
  37.             android:onClick="finish_login"  
  38.             android:background="#545bcb"  
  39.             android:textSize="20dp"  
  40.             android:textColor="#ffffff"  
  41.             android:layout_below="@+id/login_edit_pwd"  
  42.             android:layout_alignParentLeft="true"  
  43.             android:layout_alignParentStart="true"  
  44.             android:layout_marginTop="52dp" />  
  45.   
  46.         <ImageView  
  47.             android:layout_width="300dp"  
  48.             android:layout_height="150dp"  
  49.             android:id="@+id/logo"  
  50.             android:layout_alignParentRight="true"  
  51.             android:layout_alignParentEnd="true"  
  52.             android:layout_alignParentLeft="true"  
  53.             android:layout_alignParentStart="true"  
  54.             android:layout_alignParentTop="true"  
  55.             android:layout_alignWithParentIfMissing="false"  
  56.             android:background="#ffffff" />  
  57.   
  58.         <EditText  
  59.             android:layout_width="400dp"  
  60.             android:layout_height="60dp"  
  61.             android:inputType="textPassword"  
  62.             android:ems="10"  
  63.             android:id="@+id/login_edit_pwd"  
  64.             android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  65.             android:hint="请输入您的密码"  
  66.             android:layout_below="@+id/login_edit_account"  
  67.             android:layout_alignParentLeft="true"  
  68.             android:layout_alignParentStart="true" />  
  69.   
  70.         <EditText  
  71.             android:layout_width="400dp"  
  72.             android:layout_height="60dp"  
  73.             android:inputType="textPersonName"  
  74.             android:id="@+id/login_edit_account"  
  75.             android:drawableLeft="@android:drawable/ic_menu_myplaces"  
  76.             android:hint="请输入您的用户名"  
  77.             android:layout_below="@+id/logo"  
  78.             android:layout_alignParentLeft="true"  
  79.             android:layout_alignParentStart="true"  
  80.             android:layout_marginTop="20dp" />  
  81.   
  82.         <Button  
  83.             android:layout_width="fill_parent"  
  84.             android:layout_height="wrap_content"  
  85.             android:text="注销"  
  86.             android:id="@+id/login_btn_cancle"  
  87.             android:textSize="20dp"  
  88.             android:layout_below="@+id/login_btn_register"  
  89.             android:layout_marginTop="10dp"  
  90.             android:layout_alignParentRight="true"  
  91.             android:layout_alignParentEnd="true"  
  92.             android:layout_alignParentLeft="true"  
  93.             android:layout_alignParentStart="true"  
  94.             android:textColor="#ffffff"  
  95.             android:background="#3a1313" />  
  96.   
  97.         <CheckBox  
  98.             android:layout_width="100dp"  
  99.             android:layout_height="20dp"  
  100.             android:text="记住密码"  
  101.             android:id="@+id/Login_Remember"  
  102.             android:layout_below="@+id/login_edit_pwd"  
  103.             android:layout_alignParentLeft="true"  
  104.             android:layout_alignParentStart="true"  
  105.             android:checked="false"  
  106.             android:textSize="15dp" />  
  107.   
  108.         <TextView  
  109.             android:layout_width="60dp"  
  110.             android:layout_height="20dp"  
  111.             android:text="修改密码"  
  112.             android:id="@+id/login_text_change_pwd"  
  113.             android:layout_below="@+id/login_edit_pwd"  
  114.             android:layout_alignParentRight="true"  
  115.             android:layout_alignParentEnd="true"  
  116.             android:textSize="15dp" />  
  117.     </RelativeLayout>  
  118.     <RelativeLayout  
  119.         android:id="@+id/login_success_view"  
  120.         android:layout_width="fill_parent"  
  121.         android:layout_height="wrap_content"  
  122.         android:layout_centerInParent="true"  
  123.         android:layout_marginLeft="15.0px"  
  124.         android:layout_marginRight="15.0px"  
  125.         android:layout_marginTop="62.0px"  
  126.         android:background="#ff3f3f3f"  
  127.         android:paddingBottom="10.0px"  
  128.         android:paddingTop="21.0px"  
  129.         android:visibility="gone" >  
  130.   
  131.         <TextView  
  132.             android:id="@+id/login_success_show"  
  133.             android:layout_width="wrap_content"  
  134.             android:layout_height="wrap_content"  
  135.             android:layout_centerInParent="true"  
  136.             android:textColor="#ff3f3f3f"  
  137.             android:textSize="20.0dip" />  
  138.     </RelativeLayout>  
  139.   
  140. </RelativeLayout>  
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    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="com.example.foolishfan.user_v10.Login">
    <RelativeLayout
        android:id="@+id/login_view"
        android:layout_width="400dp"
        android:layout_height="800dp"
        android:layout_centerInParent="true"
         >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="注册"
            android:id="@+id/login_btn_register"
            android:onClick="resetpwd"
            android:textColor="#ffffff"
            android:background="#e52525"
            android:textSize="20dp"
            android:layout_below="@+id/login_btn_login"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="登录"
            android:id="@+id/login_btn_login"
            android:onClick="finish_login"
            android:background="#545bcb"
            android:textSize="20dp"
            android:textColor="#ffffff"
            android:layout_below="@+id/login_edit_pwd"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="52dp" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="150dp"
            android:id="@+id/logo"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="false"
            android:background="#ffffff" />

        <EditText
            android:layout_width="400dp"
            android:layout_height="60dp"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/login_edit_pwd"
            android:drawableLeft="@android:drawable/ic_lock_idle_lock"
            android:hint="请输入您的密码"
            android:layout_below="@+id/login_edit_account"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="400dp"
            android:layout_height="60dp"
            android:inputType="textPersonName"
            android:id="@+id/login_edit_account"
            android:drawableLeft="@android:drawable/ic_menu_myplaces"
            android:hint="请输入您的用户名"
            android:layout_below="@+id/logo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="20dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="注销"
            android:id="@+id/login_btn_cancle"
            android:textSize="20dp"
            android:layout_below="@+id/login_btn_register"
            android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textColor="#ffffff"
            android:background="#3a1313" />

        <CheckBox
            android:layout_width="100dp"
            android:layout_height="20dp"
            android:text="记住密码"
            android:id="@+id/Login_Remember"
            android:layout_below="@+id/login_edit_pwd"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:checked="false"
            android:textSize="15dp" />

        <TextView
            android:layout_width="60dp"
            android:layout_height="20dp"
            android:text="修改密码"
            android:id="@+id/login_text_change_pwd"
            android:layout_below="@+id/login_edit_pwd"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:textSize="15dp" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/login_success_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="15.0px"
        android:layout_marginRight="15.0px"
        android:layout_marginTop="62.0px"
        android:background="#ff3f3f3f"
        android:paddingBottom="10.0px"
        android:paddingTop="21.0px"
        android:visibility="gone" >

        <TextView
            android:id="@+id/login_success_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ff3f3f3f"
            android:textSize="20.0dip" />
    </RelativeLayout>

</RelativeLayout>
        register.xml【注册界面】
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:weightSum="1">  
  10.   
  11.     <EditText  
  12.         android:drawableLeft="@android:drawable/ic_menu_myplaces"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="60dp"  
  15.         android:inputType="textPersonName"  
  16.         android:ems="10"  
  17.         android:id="@+id/resetpwd_edit_name"  
  18.         android:layout_alignParentTop="true"  
  19.         android:hint="请输入您的用户名"  
  20.         android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"  
  21.         android:layout_alignStart="@+id/resetpwd_edit_pwd_new"  
  22.         android:layout_alignRight="@+id/resetpwd_edit_pwd_new"  
  23.         android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />  
  24.   
  25.     <!--  
  26.   
  27.     <EditText android:id="@+id/edt_operator_name" style="@style/syncEditText"  
  28.         android:hint="@string/hint_operator_name" />  
  29.     <ImageView android:id="@+id/syncOperatorImg" style="@style/syncImageView"  
  30.         android:layout_alignLeft="@+id/edt_operator_name"  
  31.         android:layout_alignTop="@+id/edt_operator_name"  
  32.         android:layout_alignBottom="@+id/edt_operator_name" android:src="@drawable/sync_operator" />  
  33. -->  
  34.   
  35.     <EditText  
  36.         android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  37.         android:layout_width="fill_parent"  
  38.         android:layout_height="60dp"  
  39.         android:inputType="textPassword"  
  40.         android:ems="10"  
  41.         android:id="@+id/resetpwd_edit_pwd_old"  
  42.         android:hint="请输入您的密码"  
  43.         android:layout_below="@+id/resetpwd_edit_name"  
  44.         android:layout_alignRight="@+id/resetpwd_edit_name"  
  45.         android:layout_alignEnd="@+id/resetpwd_edit_name"  
  46.         android:layout_alignLeft="@+id/resetpwd_edit_name"  
  47.         android:layout_alignStart="@+id/resetpwd_edit_name" />  
  48.   
  49.     <Button  
  50.         android:layout_width="fill_parent"  
  51.         android:layout_height="wrap_content"  
  52.         android:text="取消"  
  53.         android:id="@+id/register_btn_cancel"  
  54.         android:textSize="20dp"  
  55.         android:background="#f71818"  
  56.         android:layout_below="@+id/register_btn_sure"  
  57.         android:layout_alignLeft="@+id/register_btn_sure"  
  58.         android:layout_alignStart="@+id/register_btn_sure"  
  59.         android:layout_marginTop="10dp" />  
  60.   
  61.     <EditText  
  62.         android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  63.         android:layout_width="fill_parent"  
  64.         android:layout_height="60dp"  
  65.         android:inputType="textPassword"  
  66.         android:ems="10"  
  67.         android:id="@+id/resetpwd_edit_pwd_new"  
  68.         android:layout_below="@+id/resetpwd_edit_pwd_old"  
  69.         android:layout_centerHorizontal="true"  
  70.         android:hint="请确认您的密码" />  
  71.   
  72.     <Button  
  73.         android:layout_width="fill_parent"  
  74.         android:layout_height="wrap_content"  
  75.         android:text="确定"  
  76.         android:id="@+id/register_btn_sure"  
  77.         android:textSize="20dp"  
  78.         android:background="#1cf718"  
  79.         android:layout_below="@+id/resetpwd_edit_pwd_new"  
  80.         android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"  
  81.         android:layout_alignStart="@+id/resetpwd_edit_pwd_new"  
  82.         android:layout_marginTop="20dp" />  
  83.   
  84. </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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="1">

    <EditText
        android:drawableLeft="@android:drawable/ic_menu_myplaces"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/resetpwd_edit_name"
        android:layout_alignParentTop="true"
        android:hint="请输入您的用户名"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_alignRight="@+id/resetpwd_edit_pwd_new"
        android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />

    <!--

    <EditText android:id="@+id/edt_operator_name" style="@style/syncEditText"
        android:hint="@string/hint_operator_name" />
    <ImageView android:id="@+id/syncOperatorImg" style="@style/syncImageView"
        android:layout_alignLeft="@+id/edt_operator_name"
        android:layout_alignTop="@+id/edt_operator_name"
        android:layout_alignBottom="@+id/edt_operator_name" android:src="@drawable/sync_operator" />
-->

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_old"
        android:hint="请输入您的密码"
        android:layout_below="@+id/resetpwd_edit_name"
        android:layout_alignRight="@+id/resetpwd_edit_name"
        android:layout_alignEnd="@+id/resetpwd_edit_name"
        android:layout_alignLeft="@+id/resetpwd_edit_name"
        android:layout_alignStart="@+id/resetpwd_edit_name" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消"
        android:id="@+id/register_btn_cancel"
        android:textSize="20dp"
        android:background="#f71818"
        android:layout_below="@+id/register_btn_sure"
        android:layout_alignLeft="@+id/register_btn_sure"
        android:layout_alignStart="@+id/register_btn_sure"
        android:layout_marginTop="10dp" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_new"
        android:layout_below="@+id/resetpwd_edit_pwd_old"
        android:layout_centerHorizontal="true"
        android:hint="请确认您的密码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:id="@+id/register_btn_sure"
        android:textSize="20dp"
        android:background="#1cf718"
        android:layout_below="@+id/resetpwd_edit_pwd_new"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_marginTop="20dp" />

</RelativeLayout>
        resetpwd.xml【修改密码界面】
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:weightSum="1">  
  10.   
  11.     <EditText  
  12.         android:drawableLeft="@android:drawable/ic_menu_myplaces"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="60dp"  
  15.         android:inputType="textPersonName"  
  16.         android:ems="10"  
  17.         android:id="@+id/resetpwd_edit_name"  
  18.         android:layout_alignParentTop="true"  
  19.         android:hint="请输入您的用户名"  
  20.         android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"  
  21.         android:layout_alignStart="@+id/resetpwd_edit_pwd_new"  
  22.         android:layout_alignRight="@+id/resetpwd_edit_pwd_new"  
  23.         android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />  
  24.   
  25.     <!--  
  26.   
  27.         <EditText android:id="@+id/edt_operator_name" style="@style/syncEditText"  
  28.             android:hint="@string/hint_operator_name" />  
  29.         <ImageView android:id="@+id/syncOperatorImg" style="@style/syncImageView"  
  30.             android:layout_alignLeft="@+id/edt_operator_name"  
  31.             android:layout_alignTop="@+id/edt_operator_name"  
  32.             android:layout_alignBottom="@+id/edt_operator_name" android:src="@drawable/sync_operator" />  
  33.     -->  
  34.   
  35.     <EditText  
  36.         android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  37.         android:layout_width="fill_parent"  
  38.         android:layout_height="60dp"  
  39.         android:inputType="textPassword"  
  40.         android:ems="10"  
  41.         android:id="@+id/resetpwd_edit_pwd_old"  
  42.         android:hint="请输入您的旧密码"  
  43.         android:layout_below="@+id/resetpwd_edit_name"  
  44.         android:layout_alignRight="@+id/resetpwd_edit_name"  
  45.         android:layout_alignEnd="@+id/resetpwd_edit_name"  
  46.         android:layout_alignLeft="@+id/resetpwd_edit_name"  
  47.         android:layout_alignStart="@+id/resetpwd_edit_name" />  
  48.   
  49.     <Button  
  50.         android:layout_width="fill_parent"  
  51.         android:layout_height="wrap_content"  
  52.         android:text="取消"  
  53.         android:id="@+id/resetpwd_btn_cancel"  
  54.         android:textSize="20dp"  
  55.         android:onClick="not_to_reset"  
  56.         android:background="#f71818"  
  57.         android:layout_below="@+id/resetpwd_btn_sure"  
  58.         android:layout_alignParentLeft="true"  
  59.         android:layout_alignParentStart="true"  
  60.         android:layout_marginTop="20dp" />  
  61.   
  62.     <EditText  
  63.         android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  64.         android:layout_width="fill_parent"  
  65.         android:layout_height="60dp"  
  66.         android:inputType="textPassword"  
  67.         android:ems="10"  
  68.         android:id="@+id/resetpwd_edit_pwd_new"  
  69.         android:layout_below="@+id/resetpwd_edit_pwd_old"  
  70.         android:layout_centerHorizontal="true"  
  71.         android:hint="请确认您的新密码"  
  72.         />  
  73.   
  74.     <Button  
  75.         android:layout_width="fill_parent"  
  76.         android:layout_height="wrap_content"  
  77.         android:text="确定"  
  78.         android:id="@+id/resetpwd_btn_sure"  
  79.         android:textSize="20dp"  
  80.         android:onClick="sure_to_reset"  
  81.         android:background="#1cf718"  
  82.         android:layout_marginTop="92dp"  
  83.         android:layout_below="@+id/resetpwd_edit_pwd_new"  
  84.         android:layout_alignParentLeft="true"  
  85.         android:layout_alignParentStart="true" />  
  86.   
  87.     <EditText  
  88.         android:drawableLeft="@android:drawable/ic_lock_idle_lock"  
  89.         android:layout_width="fill_parent"  
  90.         android:layout_height="60dp"  
  91.         android:inputType="textPassword"  
  92.         android:ems="10"  
  93.         android:id="@+id/resetpwd_edit_pwd_check"  
  94.         android:hint="请输入您的新密码"  
  95.         android:layout_below="@+id/resetpwd_edit_pwd_new"  
  96.         android:layout_alignParentLeft="true"  
  97.         android:layout_alignParentStart="true" />  
  98.   
  99. </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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="1">

    <EditText
        android:drawableLeft="@android:drawable/ic_menu_myplaces"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/resetpwd_edit_name"
        android:layout_alignParentTop="true"
        android:hint="请输入您的用户名"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_alignRight="@+id/resetpwd_edit_pwd_new"
        android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />

    <!--

        <EditText android:id="@+id/edt_operator_name" style="@style/syncEditText"
            android:hint="@string/hint_operator_name" />
        <ImageView android:id="@+id/syncOperatorImg" style="@style/syncImageView"
            android:layout_alignLeft="@+id/edt_operator_name"
            android:layout_alignTop="@+id/edt_operator_name"
            android:layout_alignBottom="@+id/edt_operator_name" android:src="@drawable/sync_operator" />
    -->

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_old"
        android:hint="请输入您的旧密码"
        android:layout_below="@+id/resetpwd_edit_name"
        android:layout_alignRight="@+id/resetpwd_edit_name"
        android:layout_alignEnd="@+id/resetpwd_edit_name"
        android:layout_alignLeft="@+id/resetpwd_edit_name"
        android:layout_alignStart="@+id/resetpwd_edit_name" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消"
        android:id="@+id/resetpwd_btn_cancel"
        android:textSize="20dp"
        android:onClick="not_to_reset"
        android:background="#f71818"
        android:layout_below="@+id/resetpwd_btn_sure"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_new"
        android:layout_below="@+id/resetpwd_edit_pwd_old"
        android:layout_centerHorizontal="true"
        android:hint="请确认您的新密码"
        />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:id="@+id/resetpwd_btn_sure"
        android:textSize="20dp"
        android:onClick="sure_to_reset"
        android:background="#1cf718"
        android:layout_marginTop="92dp"
        android:layout_below="@+id/resetpwd_edit_pwd_new"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_check"
        android:hint="请输入您的新密码"
        android:layout_below="@+id/resetpwd_edit_pwd_new"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
        user.xml【个人中心】
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin">  
  9.   
  10.     <TextView  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:textAppearance="?android:attr/textAppearanceLarge"  
  14.         android:text="FooilshFan您好,欢迎回来!"  
  15.         android:id="@+id/textView"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginLeft="50dp"  
  18.         android:layout_marginTop="6dp"  
  19.         android:textSize="16dp" />  
  20.   
  21.     <Button  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="退出"  
  25.         android:id="@+id/returnback"  
  26.         android:layout_below="@+id/textView"  
  27.         android:layout_marginTop="36dp"  
  28.         android:onClick="back_to_login"  
  29.         android:textColor="#ffffff"  
  30.         android:textSize="20dp"  
  31.         android:background="#d95b5b"  
  32.         android:layout_alignParentLeft="true"  
  33.         android:layout_alignParentStart="true"  
  34.         android:layout_alignParentRight="true"  
  35.         android:layout_alignParentEnd="true" />  
  36.   
  37. </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="match_parent"
    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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="FooilshFan您好,欢迎回来!"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="6dp"
        android:textSize="16dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出"
        android:id="@+id/returnback"
        android:layout_below="@+id/textView"
        android:layout_marginTop="36dp"
        android:onClick="back_to_login"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:background="#d95b5b"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

2:用户数据管理的类

       UserData.java 【用户数据】
  1. package com.example.foolishfan.user_v10;  
  2. public class UserData {  
  3.     private String userName;                  //用户名  
  4.     private String userPwd;                   //用户密码  
  5.     private int userId;                       //用户ID号  
  6.     public int pwdresetFlag=0;  
  7.     //获取用户名  
  8.     public String getUserName() {             //获取用户名  
  9.         return userName;  
  10.     }  
  11.     //设置用户名  
  12.     public void setUserName(String userName) {  //输入用户名  
  13.         this.userName = userName;  
  14.     }  
  15.     //获取用户密码  
  16.     public String getUserPwd() {                //获取用户密码  
  17.         return userPwd;  
  18.     }  
  19.     //设置用户密码  
  20.     public void setUserPwd(String userPwd) {     //输入用户密码  
  21.         this.userPwd = userPwd;  
  22.     }  
  23.     //获取用户id  
  24.     public int getUserId() {                   //获取用户ID号  
  25.         return userId;  
  26.     }  
  27.     //设置用户id  
  28.     public void setUserId(int userId) {       //设置用户ID号  
  29.         this.userId = userId;  
  30.     }  
  31.     public UserData(String userName, String userPwd) {  //这里只采用用户名和密码  
  32.         super();  
  33.         this.userName = userName;  
  34.         this.userPwd = userPwd;  
  35.     }  
  36.   
  37. }  
package com.example.foolishfan.user_v10;
public class UserData {
    private String userName;                  //用户名
    private String userPwd;                   //用户密码
    private int userId;                       //用户ID号
    public int pwdresetFlag=0;
    //获取用户名
    public String getUserName() {             //获取用户名
        return userName;
    }
    //设置用户名
    public void setUserName(String userName) {  //输入用户名
        this.userName = userName;
    }
    //获取用户密码
    public String getUserPwd() {                //获取用户密码
        return userPwd;
    }
    //设置用户密码
    public void setUserPwd(String userPwd) {     //输入用户密码
        this.userPwd = userPwd;
    }
    //获取用户id
    public int getUserId() {                   //获取用户ID号
        return userId;
    }
    //设置用户id
    public void setUserId(int userId) {       //设置用户ID号
        this.userId = userId;
    }
    public UserData(String userName, String userPwd) {  //这里只采用用户名和密码
        super();
        this.userName = userName;
        this.userPwd = userPwd;
    }

}
       UserDataManager.java【用户管理】
  1. package com.example.foolishfan.user_v10;  
  2. import android.content.ContentValues;  
  3. import android.content.Context;  
  4. import android.database.Cursor;  
  5. import android.database.SQLException;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteOpenHelper;  
  8. import android.util.Log;  
  9.   
  10. public class UserDataManager {             //用户数据管理类  
  11.     //一些宏定义和声明  
  12.     private static final String TAG = "UserDataManager";  
  13.     private static final String DB_NAME = "user_data";  
  14.     private static final String TABLE_NAME = "users";  
  15.     public static final String ID = "_id";  
  16.     public static final String USER_NAME = "user_name";  
  17.     public static final String USER_PWD = "user_pwd";  
  18. //    public static final String SILENT = "silent";  
  19. //    public static final String VIBRATE = "vibrate";  
  20.     private static final int DB_VERSION = 2;  
  21.     private Context mContext = null;  
  22.   
  23.     //创建用户book表  
  24.     private static final String DB_CREATE = "CREATE TABLE " + TABLE_NAME + " ("  
  25.             + ID + " integer primary key," + USER_NAME + " varchar,"  
  26.             + USER_PWD + " varchar" + ");";  
  27.   
  28.     private SQLiteDatabase mSQLiteDatabase = null;  
  29.     private DataBaseManagementHelper mDatabaseHelper = null;  
  30.   
  31.     //DataBaseManagementHelper继承自SQLiteOpenHelper  
  32.     private static class DataBaseManagementHelper extends SQLiteOpenHelper {  
  33.   
  34.         DataBaseManagementHelper(Context context) {  
  35.             super(context, DB_NAME, null, DB_VERSION);  
  36.         }  
  37.   
  38.         @Override  
  39.         public void onCreate(SQLiteDatabase db) {  
  40.             Log.i(TAG,"db.getVersion()="+db.getVersion());  
  41.             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");  
  42.             db.execSQL(DB_CREATE);  
  43.             Log.i(TAG, "db.execSQL(DB_CREATE)");  
  44.             Log.e(TAG, DB_CREATE);  
  45.         }  
  46.   
  47.         @Override  
  48.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  49.             Log.i(TAG, "DataBaseManagementHelper onUpgrade");  
  50.             onCreate(db);  
  51.         }  
  52.     }  
  53.   
  54.     public UserDataManager(Context context) {  
  55.         mContext = context;  
  56.         Log.i(TAG, "UserDataManager construction!");  
  57.     }  
  58.     //打开数据库  
  59.     public void openDataBase() throws SQLException {  
  60.         mDatabaseHelper = new DataBaseManagementHelper(mContext);  
  61.         mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();  
  62.     }  
  63.     //关闭数据库  
  64.     public void closeDataBase() throws SQLException {  
  65.         mDatabaseHelper.close();  
  66.     }  
  67.     //添加新用户,即注册  
  68.     public long insertUserData(UserData userData) {  
  69.         String userName=userData.getUserName();  
  70.         String userPwd=userData.getUserPwd();  
  71.         ContentValues values = new ContentValues();  
  72.         values.put(USER_NAME, userName);  
  73.         values.put(USER_PWD, userPwd);  
  74.         return mSQLiteDatabase.insert(TABLE_NAME, ID, values);  
  75.     }  
  76.     //更新用户信息,如修改密码  
  77.     public boolean updateUserData(UserData userData) {  
  78.         //int id = userData.getUserId();  
  79.         String userName = userData.getUserName();  
  80.         String userPwd = userData.getUserPwd();  
  81.         ContentValues values = new ContentValues();  
  82.         values.put(USER_NAME, userName);  
  83.         values.put(USER_PWD, userPwd);  
  84.         return mSQLiteDatabase.update(TABLE_NAME, values,nullnull) > 0;  
  85.         //return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;  
  86.     }  
  87.     //  
  88.     public Cursor fetchUserData(int id) throws SQLException {  
  89.         Cursor mCursor = mSQLiteDatabase.query(false, TABLE_NAME, null, ID  
  90.                 + "=" + id, nullnullnullnullnull);  
  91.         if (mCursor != null) {  
  92.             mCursor.moveToFirst();  
  93.         }  
  94.         return mCursor;  
  95.     }  
  96.     //  
  97.     public Cursor fetchAllUserDatas() {  
  98.         return mSQLiteDatabase.query(TABLE_NAME, nullnullnullnullnull,  
  99.                 null);  
  100.     }  
  101.     //根据id删除用户  
  102.     public boolean deleteUserData(int id) {  
  103.         return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;  
  104.     }  
  105.     //根据用户名注销  
  106.     public boolean deleteUserDatabyname(String name) {  
  107.         return mSQLiteDatabase.delete(TABLE_NAME, USER_NAME + "=" + name, null) > 0;  
  108.     }  
  109.     //删除所有用户  
  110.     public boolean deleteAllUserDatas() {  
  111.         return mSQLiteDatabase.delete(TABLE_NAME, nullnull) > 0;  
  112.     }  
  113.   
  114.     //  
  115.     public String getStringByColumnName(String columnName, int id) {  
  116.         Cursor mCursor = fetchUserData(id);  
  117.         int columnIndex = mCursor.getColumnIndex(columnName);  
  118.         String columnValue = mCursor.getString(columnIndex);  
  119.         mCursor.close();  
  120.         return columnValue;  
  121.     }  
  122.     //  
  123.     public boolean updateUserDataById(String columnName, int id,  
  124.                                       String columnValue) {  
  125.         ContentValues values = new ContentValues();  
  126.         values.put(columnName, columnValue);  
  127.         return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;  
  128.     }  
  129.     //根据用户名找用户,可以判断注册时用户名是否已经存在  
  130.     public int findUserByName(String userName){  
  131.         Log.i(TAG,"findUserByName , userName="+userName);  
  132.         int result=0;  
  133.         Cursor mCursor=mSQLiteDatabase.query(TABLE_NAME, null, USER_NAME+"="+userName, nullnullnullnull);  
  134.         if(mCursor!=null){  
  135.             result=mCursor.getCount();  
  136.             mCursor.close();  
  137.             Log.i(TAG,"findUserByName , result="+result);  
  138.         }  
  139.         return result;  
  140.     }  
  141.     //根据用户名和密码找用户,用于登录  
  142.     public int findUserByNameAndPwd(String userName,String pwd){  
  143.         Log.i(TAG,"findUserByNameAndPwd");  
  144.         int result=0;  
  145.         Cursor mCursor=mSQLiteDatabase.query(TABLE_NAME, null, USER_NAME+"="+userName+" and "+USER_PWD+"="+pwd,  
  146.                 nullnullnullnull);  
  147.         if(mCursor!=null){  
  148.             result=mCursor.getCount();  
  149.             mCursor.close();  
  150.             Log.i(TAG,"findUserByNameAndPwd , result="+result);  
  151.         }  
  152.         return result;  
  153.     }  
  154.   
  155. }  
package com.example.foolishfan.user_v10;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class UserDataManager {             //用户数据管理类
    //一些宏定义和声明
    private static final String TAG = "UserDataManager";
    private static final String DB_NAME = "user_data";
    private static final String TABLE_NAME = "users";
    public static final String ID = "_id";
    public static final String USER_NAME = "user_name";
    public static final String USER_PWD = "user_pwd";
//    public static final String SILENT = "silent";
//    public static final String VIBRATE = "vibrate";
    private static final int DB_VERSION = 2;
    private Context mContext = null;

    //创建用户book表
    private static final String DB_CREATE = "CREATE TABLE " + TABLE_NAME + " ("
            + ID + " integer primary key," + USER_NAME + " varchar,"
            + USER_PWD + " varchar" + ");";

    private SQLiteDatabase mSQLiteDatabase = null;
    private DataBaseManagementHelper mDatabaseHelper = null;

    //DataBaseManagementHelper继承自SQLiteOpenHelper
    private static class DataBaseManagementHelper extends SQLiteOpenHelper {

        DataBaseManagementHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG,"db.getVersion()="+db.getVersion());
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
            db.execSQL(DB_CREATE);
            Log.i(TAG, "db.execSQL(DB_CREATE)");
            Log.e(TAG, DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i(TAG, "DataBaseManagementHelper onUpgrade");
            onCreate(db);
        }
    }

    public UserDataManager(Context context) {
        mContext = context;
        Log.i(TAG, "UserDataManager construction!");
    }
    //打开数据库
    public void openDataBase() throws SQLException {
        mDatabaseHelper = new DataBaseManagementHelper(mContext);
        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
    }
    //关闭数据库
    public void closeDataBase() throws SQLException {
        mDatabaseHelper.close();
    }
    //添加新用户,即注册
    public long insertUserData(UserData userData) {
        String userName=userData.getUserName();
        String userPwd=userData.getUserPwd();
        ContentValues values = new ContentValues();
        values.put(USER_NAME, userName);
        values.put(USER_PWD, userPwd);
        return mSQLiteDatabase.insert(TABLE_NAME, ID, values);
    }
    //更新用户信息,如修改密码
    public boolean updateUserData(UserData userData) {
        //int id = userData.getUserId();
        String userName = userData.getUserName();
        String userPwd = userData.getUserPwd();
        ContentValues values = new ContentValues();
        values.put(USER_NAME, userName);
        values.put(USER_PWD, userPwd);
        return mSQLiteDatabase.update(TABLE_NAME, values,null, null) > 0;
        //return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
    }
    //
    public Cursor fetchUserData(int id) throws SQLException {
        Cursor mCursor = mSQLiteDatabase.query(false, TABLE_NAME, null, ID
                + "=" + id, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    //
    public Cursor fetchAllUserDatas() {
        return mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null,
                null);
    }
    //根据id删除用户
    public boolean deleteUserData(int id) {
        return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;
    }
    //根据用户名注销
    public boolean deleteUserDatabyname(String name) {
        return mSQLiteDatabase.delete(TABLE_NAME, USER_NAME + "=" + name, null) > 0;
    }
    //删除所有用户
    public boolean deleteAllUserDatas() {
        return mSQLiteDatabase.delete(TABLE_NAME, null, null) > 0;
    }

    //
    public String getStringByColumnName(String columnName, int id) {
        Cursor mCursor = fetchUserData(id);
        int columnIndex = mCursor.getColumnIndex(columnName);
        String columnValue = mCursor.getString(columnIndex);
        mCursor.close();
        return columnValue;
    }
    //
    public boolean updateUserDataById(String columnName, int id,
                                      String columnValue) {
        ContentValues values = new ContentValues();
        values.put(columnName, columnValue);
        return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
    }
    //根据用户名找用户,可以判断注册时用户名是否已经存在
    public int findUserByName(String userName){
        Log.i(TAG,"findUserByName , userName="+userName);
        int result=0;
        Cursor mCursor=mSQLiteDatabase.query(TABLE_NAME, null, USER_NAME+"="+userName, null, null, null, null);
        if(mCursor!=null){
            result=mCursor.getCount();
            mCursor.close();
            Log.i(TAG,"findUserByName , result="+result);
        }
        return result;
    }
    //根据用户名和密码找用户,用于登录
    public int findUserByNameAndPwd(String userName,String pwd){
        Log.i(TAG,"findUserByNameAndPwd");
        int result=0;
        Cursor mCursor=mSQLiteDatabase.query(TABLE_NAME, null, USER_NAME+"="+userName+" and "+USER_PWD+"="+pwd,
                null, null, null, null);
        if(mCursor!=null){
            result=mCursor.getCount();
            mCursor.close();
            Log.i(TAG,"findUserByNameAndPwd , result="+result);
        }
        return result;
    }

}

3:具体操作的实现代码,即各个activity

        Login.java【用户登录activity】
  1. package com.example.foolishfan.user_v10;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.CheckBox;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class Login extends Activity {                 //登录界面活动  
  17.   
  18.     public int pwdresetFlag=0;  
  19.     private EditText mAccount;                        //用户名编辑  
  20.     private EditText mPwd;                            //密码编辑  
  21.     private Button mRegisterButton;                   //注册按钮  
  22.     private Button mLoginButton;                      //登录按钮  
  23.     private Button mCancleButton;                     //注销按钮  
  24.     private CheckBox mRememberCheck;  
  25.   
  26.     private SharedPreferences login_sp;  
  27.     private String userNameValue,passwordValue;  
  28.   
  29.     private View loginView;                           //登录  
  30.     private View loginSuccessView;  
  31.     private TextView loginSuccessShow;  
  32.     private TextView mChangepwdText;  
  33.     private UserDataManager mUserDataManager;         //用户数据管理类  
  34.   
  35.   
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.login);  
  40.         //通过id找到相应的控件  
  41.         mAccount = (EditText) findViewById(R.id.login_edit_account);  
  42.         mPwd = (EditText) findViewById(R.id.login_edit_pwd);  
  43.         mRegisterButton = (Button) findViewById(R.id.login_btn_register);  
  44.         mLoginButton = (Button) findViewById(R.id.login_btn_login);  
  45.         mCancleButton = (Button) findViewById(R.id.login_btn_cancle);  
  46.         loginView=findViewById(R.id.login_view);  
  47.         loginSuccessView=findViewById(R.id.login_success_view);  
  48.         loginSuccessShow=(TextView) findViewById(R.id.login_success_show);  
  49.   
  50.         mChangepwdText = (TextView) findViewById(R.id.login_text_change_pwd);  
  51.   
  52.         mRememberCheck = (CheckBox) findViewById(R.id.Login_Remember);  
  53.   
  54.         login_sp = getSharedPreferences("userInfo"0);  
  55.         String name=login_sp.getString("USER_NAME""");  
  56.         String pwd =login_sp.getString("PASSWORD""");  
  57.         boolean choseRemember =login_sp.getBoolean("mRememberCheck"false);  
  58.         boolean choseAutoLogin =login_sp.getBoolean("mAutologinCheck"false);  
  59.         //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码  
  60.         if(choseRemember){  
  61.             mAccount.setText(name);  
  62.             mPwd.setText(pwd);  
  63.             mRememberCheck.setChecked(true);  
  64.         }  
  65.   
  66.         mRegisterButton.setOnClickListener(mListener);                      //采用OnClickListener方法设置不同按钮按下之后的监听事件  
  67.         mLoginButton.setOnClickListener(mListener);  
  68.         mCancleButton.setOnClickListener(mListener);  
  69.         mChangepwdText.setOnClickListener(mListener);  
  70.   
  71.         ImageView image = (ImageView) findViewById(R.id.logo);             //使用ImageView显示logo  
  72.         image.setImageResource(R.drawable.logo);  
  73.   
  74.         if (mUserDataManager == null) {  
  75.             mUserDataManager = new UserDataManager(this);  
  76.             mUserDataManager.openDataBase();                              //建立本地数据库  
  77.         }  
  78.     }  
  79.     OnClickListener mListener = new OnClickListener() {                  //不同按钮按下的监听事件选择  
  80.         public void onClick(View v) {  
  81.             switch (v.getId()) {  
  82.                 case R.id.login_btn_register:                            //登录界面的注册按钮  
  83.                     Intent intent_Login_to_Register = new Intent(Login.this,Register.class) ;    //切换Login Activity至User Activity  
  84.                     startActivity(intent_Login_to_Register);  
  85.                     finish();  
  86.                     break;  
  87.                 case R.id.login_btn_login:                              //登录界面的登录按钮  
  88.                     login();  
  89.                     break;  
  90.                 case R.id.login_btn_cancle:                             //登录界面的注销按钮  
  91.                     cancel();  
  92.                     break;  
  93.                 case R.id.login_text_change_pwd:                             //登录界面的注销按钮  
  94.                     Intent intent_Login_to_reset = new Intent(Login.this,Resetpwd.class) ;    //切换Login Activity至User Activity  
  95.                     startActivity(intent_Login_to_reset);  
  96.                     finish();  
  97.                     break;  
  98.             }  
  99.         }  
  100.     };  
  101.   
  102.     public void login() {                                              //登录按钮监听事件  
  103.         if (isUserNameAndPwdValid()) {  
  104.             String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息  
  105.             String userPwd = mPwd.getText().toString().trim();  
  106.             SharedPreferences.Editor editor =login_sp.edit();  
  107.             int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);  
  108.             if(result==1){                                             //返回1说明用户名和密码均正确  
  109.                 //保存用户名和密码  
  110.                 editor.putString("USER_NAME", userName);  
  111.                 editor.putString("PASSWORD", userPwd);  
  112.   
  113.                 //是否记住密码  
  114.                 if(mRememberCheck.isChecked()){  
  115.                     editor.putBoolean("mRememberCheck"true);  
  116.                 }else{  
  117.                     editor.putBoolean("mRememberCheck"false);  
  118.                 }  
  119.                 editor.commit();  
  120.   
  121.                 Intent intent = new Intent(Login.this,User.class) ;    //切换Login Activity至User Activity  
  122.                 startActivity(intent);  
  123.                 finish();  
  124.                 Toast.makeText(this, getString(R.string.login_success),Toast.LENGTH_SHORT).show();//登录成功提示  
  125.             }else if(result==0){  
  126.                 Toast.makeText(this, getString(R.string.login_fail),Toast.LENGTH_SHORT).show();  //登录失败提示  
  127.             }  
  128.         }  
  129.     }  
  130.     public void cancel() {           //注销  
  131.         if (isUserNameAndPwdValid()) {  
  132.             String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息  
  133.             String userPwd = mPwd.getText().toString().trim();  
  134.             int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);  
  135.             if(result==1){                                             //返回1说明用户名和密码均正确  
  136.                 Toast.makeText(this, getString(R.string.cancel_success),Toast.LENGTH_SHORT).show();<span style="font-family: Arial;">//注销成功提示</span>  
  137.                 mPwd.setText("");  
  138.                 mAccount.setText("");  
  139.                 mUserDataManager.deleteUserDatabyname(userName);  
  140.             }else if(result==0){  
  141.                 Toast.makeText(this, getString(R.string.cancel_fail),Toast.LENGTH_SHORT).show();  //注销失败提示  
  142.             }  
  143.         }  
  144.   
  145.     }  
  146.     public boolean isUserNameAndPwdValid() {  
  147.         if (mAccount.getText().toString().trim().equals("")) {  
  148.             Toast.makeText(this, getString(R.string.account_empty),  
  149.                     Toast.LENGTH_SHORT).show();  
  150.             return false;  
  151.         } else if (mPwd.getText().toString().trim().equals("")) {  
  152.             Toast.makeText(this, getString(R.string.pwd_empty),  
  153.                     Toast.LENGTH_SHORT).show();  
  154.             return false;  
  155.         }  
  156.         return true;  
  157.     }  
  158.     @Override  
  159.     protected void onResume() {  
  160.         if (mUserDataManager == null) {  
  161.             mUserDataManager = new UserDataManager(this);  
  162.             mUserDataManager.openDataBase();  
  163.         }  
  164.         super.onResume();  
  165.     }  
  166.     @Override  
  167.     protected void onDestroy() {  
  168.         super.onDestroy();  
  169.     }  
  170.     @Override  
  171.     protected void onPause() {  
  172.         if (mUserDataManager != null) {  
  173.             mUserDataManager.closeDataBase();  
  174.             mUserDataManager = null;  
  175.         }  
  176.         super.onPause();  
  177.     }  
  178. }  
package com.example.foolishfan.user_v10;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {                 //登录界面活动

    public int pwdresetFlag=0;
    private EditText mAccount;                        //用户名编辑
    private EditText mPwd;                            //密码编辑
    private Button mRegisterButton;                   //注册按钮
    private Button mLoginButton;                      //登录按钮
    private Button mCancleButton;                     //注销按钮
    private CheckBox mRememberCheck;

    private SharedPreferences login_sp;
    private String userNameValue,passwordValue;

    private View loginView;                           //登录
    private View loginSuccessView;
    private TextView loginSuccessShow;
    private TextView mChangepwdText;
    private UserDataManager mUserDataManager;         //用户数据管理类


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //通过id找到相应的控件
        mAccount = (EditText) findViewById(R.id.login_edit_account);
        mPwd = (EditText) findViewById(R.id.login_edit_pwd);
        mRegisterButton = (Button) findViewById(R.id.login_btn_register);
        mLoginButton = (Button) findViewById(R.id.login_btn_login);
        mCancleButton = (Button) findViewById(R.id.login_btn_cancle);
        loginView=findViewById(R.id.login_view);
        loginSuccessView=findViewById(R.id.login_success_view);
        loginSuccessShow=(TextView) findViewById(R.id.login_success_show);

        mChangepwdText = (TextView) findViewById(R.id.login_text_change_pwd);

        mRememberCheck = (CheckBox) findViewById(R.id.Login_Remember);

        login_sp = getSharedPreferences("userInfo", 0);
        String name=login_sp.getString("USER_NAME", "");
        String pwd =login_sp.getString("PASSWORD", "");
        boolean choseRemember =login_sp.getBoolean("mRememberCheck", false);
        boolean choseAutoLogin =login_sp.getBoolean("mAutologinCheck", false);
        //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
        if(choseRemember){
            mAccount.setText(name);
            mPwd.setText(pwd);
            mRememberCheck.setChecked(true);
        }

        mRegisterButton.setOnClickListener(mListener);                      //采用OnClickListener方法设置不同按钮按下之后的监听事件
        mLoginButton.setOnClickListener(mListener);
        mCancleButton.setOnClickListener(mListener);
        mChangepwdText.setOnClickListener(mListener);

        ImageView image = (ImageView) findViewById(R.id.logo);             //使用ImageView显示logo
        image.setImageResource(R.drawable.logo);

        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }
    }
    OnClickListener mListener = new OnClickListener() {                  //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login_btn_register:                            //登录界面的注册按钮
                    Intent intent_Login_to_Register = new Intent(Login.this,Register.class) ;    //切换Login Activity至User Activity
                    startActivity(intent_Login_to_Register);
                    finish();
                    break;
                case R.id.login_btn_login:                              //登录界面的登录按钮
                    login();
                    break;
                case R.id.login_btn_cancle:                             //登录界面的注销按钮
                    cancel();
                    break;
                case R.id.login_text_change_pwd:                             //登录界面的注销按钮
                    Intent intent_Login_to_reset = new Intent(Login.this,Resetpwd.class) ;    //切换Login Activity至User Activity
                    startActivity(intent_Login_to_reset);
                    finish();
                    break;
            }
        }
    };

    public void login() {                                              //登录按钮监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息
            String userPwd = mPwd.getText().toString().trim();
            SharedPreferences.Editor editor =login_sp.edit();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
            if(result==1){                                             //返回1说明用户名和密码均正确
                //保存用户名和密码
                editor.putString("USER_NAME", userName);
                editor.putString("PASSWORD", userPwd);

                //是否记住密码
                if(mRememberCheck.isChecked()){
                    editor.putBoolean("mRememberCheck", true);
                }else{
                    editor.putBoolean("mRememberCheck", false);
                }
                editor.commit();

                Intent intent = new Intent(Login.this,User.class) ;    //切换Login Activity至User Activity
                startActivity(intent);
                finish();
                Toast.makeText(this, getString(R.string.login_success),Toast.LENGTH_SHORT).show();//登录成功提示
            }else if(result==0){
                Toast.makeText(this, getString(R.string.login_fail),Toast.LENGTH_SHORT).show();  //登录失败提示
            }
        }
    }
    public void cancel() {           //注销
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息
            String userPwd = mPwd.getText().toString().trim();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
            if(result==1){                                             //返回1说明用户名和密码均正确
                Toast.makeText(this, getString(R.string.cancel_success),Toast.LENGTH_SHORT).show();<span style="font-family: Arial;">//注销成功提示</span>
                mPwd.setText("");
                mAccount.setText("");
                mUserDataManager.deleteUserDatabyname(userName);
            }else if(result==0){
                Toast.makeText(this, getString(R.string.cancel_fail),Toast.LENGTH_SHORT).show();  //注销失败提示
            }
        }

    }
    public boolean isUserNameAndPwdValid() {
        if (mAccount.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.account_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (mPwd.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    @Override
    protected void onResume() {
        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();
        }
        super.onResume();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void onPause() {
        if (mUserDataManager != null) {
            mUserDataManager.closeDataBase();
            mUserDataManager = null;
        }
        super.onPause();
    }
}
       Register.java【用户注册activity】
  1. package com.example.foolishfan.user_v10;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11.   
  12. public class Register extends AppCompatActivity {  
  13.     private EditText mAccount;                        //用户名编辑  
  14.     private EditText mPwd;                            //密码编辑  
  15.     private EditText mPwdCheck;                       //密码编辑  
  16.     private Button mSureButton;                       //确定按钮  
  17.     private Button mCancelButton;                     //取消按钮  
  18.     private UserDataManager mUserDataManager;         //用户数据管理类  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.register);  
  23.         mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);  
  24.         mPwd = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);  
  25.         mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);  
  26.   
  27.         mSureButton = (Button) findViewById(R.id.register_btn_sure);  
  28.         mCancelButton = (Button) findViewById(R.id.register_btn_cancel);  
  29.   
  30.         mSureButton.setOnClickListener(m_register_Listener);      //注册界面两个按钮的监听事件  
  31.         mCancelButton.setOnClickListener(m_register_Listener);  
  32.   
  33.         if (mUserDataManager == null) {  
  34.             mUserDataManager = new UserDataManager(this);  
  35.             mUserDataManager.openDataBase();                              //建立本地数据库  
  36.         }  
  37.     }  
  38.     View.OnClickListener m_register_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择  
  39.         public void onClick(View v) {  
  40.             switch (v.getId()) {  
  41.                 case R.id.register_btn_sure:                       //确认按钮的监听事件  
  42.                     register_check();  
  43.                     break;  
  44.                 case R.id.register_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面  
  45.                     Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity  
  46.                     startActivity(intent_Register_to_Login);  
  47.                     finish();  
  48.                     break;  
  49.             }  
  50.         }  
  51.     };  
  52.     public void register_check() {                                //确认按钮的监听事件  
  53.         if (isUserNameAndPwdValid()) {  
  54.             String userName = mAccount.getText().toString().trim();  
  55.             String userPwd = mPwd.getText().toString().trim();  
  56.             String userPwdCheck = mPwdCheck.getText().toString().trim();  
  57.             //检查用户是否存在  
  58.             int count=mUserDataManager.findUserByName(userName);  
  59.             //用户已经存在时返回,给出提示文字  
  60.             if(count>0){  
  61.                 Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();  
  62.                 return ;  
  63.             }  
  64.             if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样  
  65.                 Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
  66.                 return ;  
  67.             } else {  
  68.                 UserData mUser = new UserData(userName, userPwd);  
  69.                 mUserDataManager.openDataBase();  
  70.                 long flag = mUserDataManager.insertUserData(mUser); //新建用户信息  
  71.                 if (flag == -1) {  
  72.                     Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();  
  73.                 }else{  
  74.                     Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();  
  75.                     Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity  
  76.                     startActivity(intent_Register_to_Login);  
  77.                     finish();  
  78.                 }  
  79.             }  
  80.         }  
  81.     }  
  82.     public boolean isUserNameAndPwdValid() {  
  83.         if (mAccount.getText().toString().trim().equals("")) {  
  84.             Toast.makeText(this, getString(R.string.account_empty),  
  85.                     Toast.LENGTH_SHORT).show();  
  86.             return false;  
  87.         } else if (mPwd.getText().toString().trim().equals("")) {  
  88.             Toast.makeText(this, getString(R.string.pwd_empty),  
  89.                     Toast.LENGTH_SHORT).show();  
  90.             return false;  
  91.         }else if(mPwdCheck.getText().toString().trim().equals("")) {  
  92.             Toast.makeText(this, getString(R.string.pwd_check_empty),  
  93.                     Toast.LENGTH_SHORT).show();  
  94.             return false;  
  95.         }  
  96.         return true;  
  97.     }  
  98. }  
package com.example.foolishfan.user_v10;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    private EditText mAccount;                        //用户名编辑
    private EditText mPwd;                            //密码编辑
    private EditText mPwdCheck;                       //密码编辑
    private Button mSureButton;                       //确定按钮
    private Button mCancelButton;                     //取消按钮
    private UserDataManager mUserDataManager;         //用户数据管理类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);
        mPwd = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);
        mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);

        mSureButton = (Button) findViewById(R.id.register_btn_sure);
        mCancelButton = (Button) findViewById(R.id.register_btn_cancel);

        mSureButton.setOnClickListener(m_register_Listener);      //注册界面两个按钮的监听事件
        mCancelButton.setOnClickListener(m_register_Listener);

        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }
    }
    View.OnClickListener m_register_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.register_btn_sure:                       //确认按钮的监听事件
                    register_check();
                    break;
                case R.id.register_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                    break;
            }
        }
    };
    public void register_check() {                                //确认按钮的监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd = mPwd.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            //检查用户是否存在
            int count=mUserDataManager.findUserByName(userName);
            //用户已经存在时返回,给出提示文字
            if(count>0){
                Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();
                return ;
            }
            if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样
                Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                return ;
            } else {
                UserData mUser = new UserData(userName, userPwd);
                mUserDataManager.openDataBase();
                long flag = mUserDataManager.insertUserData(mUser); //新建用户信息
                if (flag == -1) {
                    Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                }
            }
        }
    }
    public boolean isUserNameAndPwdValid() {
        if (mAccount.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.account_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (mPwd.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwdCheck.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_check_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
        Resetpwd.java【重新设置密码】
  1. package com.example.foolishfan.user_v10;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.RelativeLayout;  
  10. import android.widget.Toast;  
  11.   
  12. public class Resetpwd extends AppCompatActivity {  
  13.     private EditText mAccount;                        //用户名编辑  
  14.     private EditText mPwd_old;                        //密码编辑  
  15.     private EditText mPwd_new;                        //密码编辑  
  16.     private EditText mPwdCheck;                       //密码编辑  
  17.     private Button mSureButton;                       //确定按钮  
  18.     private Button mCancelButton;                     //取消按钮  
  19.     private UserDataManager mUserDataManager;         //用户数据管理类  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.resetpwd);  
  24.         mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);  
  25.         mPwd_old = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);  
  26.         mPwd_new = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);  
  27.         mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_check);  
  28.   
  29.         mSureButton = (Button) findViewById(R.id.resetpwd_btn_sure);  
  30.         mCancelButton = (Button) findViewById(R.id.resetpwd_btn_cancel);  
  31.   
  32.         mSureButton.setOnClickListener(m_resetpwd_Listener);      //注册界面两个按钮的监听事件  
  33.         mCancelButton.setOnClickListener(m_resetpwd_Listener);  
  34.         if (mUserDataManager == null) {  
  35.             mUserDataManager = new UserDataManager(this);  
  36.             mUserDataManager.openDataBase();                              //建立本地数据库  
  37.         }  
  38.     }  
  39.     View.OnClickListener m_resetpwd_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择  
  40.         public void onClick(View v) {  
  41.             switch (v.getId()) {  
  42.                 case R.id.resetpwd_btn_sure:                       //确认按钮的监听事件  
  43.                     resetpwd_check();  
  44.                     break;  
  45.                 case R.id.resetpwd_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面  
  46.                     Intent intent_Resetpwd_to_Login = new Intent(Resetpwd.this,Login.class) ;    //切换Resetpwd Activity至Login Activity  
  47.                     startActivity(intent_Resetpwd_to_Login);  
  48.                     finish();  
  49.                     break;  
  50.             }  
  51.         }  
  52.     };  
  53.     public void resetpwd_check() {                                //确认按钮的监听事件  
  54.         if (isUserNameAndPwdValid()) {  
  55.             String userName = mAccount.getText().toString().trim();  
  56.             String userPwd_old = mPwd_old.getText().toString().trim();  
  57.             String userPwd_new = mPwd_new.getText().toString().trim();  
  58.             String userPwdCheck = mPwdCheck.getText().toString().trim();  
  59.             int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd_old);  
  60.             if(result==1){                                             //返回1说明用户名和密码均正确,继续后续操作  
  61.                 if(userPwd_new.equals(userPwdCheck)==false){           //两次密码输入不一样  
  62.                     Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
  63.                     return ;  
  64.                 } else {  
  65.                     UserData mUser = new UserData(userName, userPwd_new);  
  66.                     mUserDataManager.openDataBase();  
  67.                     boolean flag = mUserDataManager.updateUserData(mUser);  
  68.                     if (flag == false) {  
  69.                         Toast.makeText(this, getString(R.string.resetpwd_fail),Toast.LENGTH_SHORT).show();  
  70.                     }else{  
  71.                         Toast.makeText(this, getString(R.string.resetpwd_success),Toast.LENGTH_SHORT).show();  
  72.                         mUser.pwdresetFlag=1;  
  73.                         Intent intent_Register_to_Login = new Intent(Resetpwd.this,Login.class) ;    //切换User Activity至Login Activity  
  74.                         startActivity(intent_Register_to_Login);  
  75.                         finish();  
  76.                     }  
  77.                 }  
  78.             }else if(result==0){                                       //返回0说明用户名和密码不匹配,重新输入  
  79.                 Toast.makeText(this, getString(R.string.pwd_not_fit_user),Toast.LENGTH_SHORT).show();  
  80.                 return;  
  81.             }  
  82.         }  
  83.     }  
  84.     public boolean isUserNameAndPwdValid() {  
  85.         String userName = mAccount.getText().toString().trim();  
  86.         //检查用户是否存在  
  87.         int count=mUserDataManager.findUserByName(userName);  
  88.         //用户不存在时返回,给出提示文字  
  89.         if(count<=0){  
  90.             Toast.makeText(this, getString(R.string.name_not_exist, userName),Toast.LENGTH_SHORT).show();  
  91.             return false;  
  92.         }  
  93.         if (mAccount.getText().toString().trim().equals("")) {  
  94.             Toast.makeText(this, getString(R.string.account_empty),Toast.LENGTH_SHORT).show();  
  95.             return false;  
  96.         } else if (mPwd_old.getText().toString().trim().equals("")) {  
  97.             Toast.makeText(this, getString(R.string.pwd_empty),Toast.LENGTH_SHORT).show();  
  98.             return false;  
  99.         } else if (mPwd_new.getText().toString().trim().equals("")) {  
  100.             Toast.makeText(this, getString(R.string.pwd_new_empty),Toast.LENGTH_SHORT).show();  
  101.             return false;  
  102.         }else if(mPwdCheck.getText().toString().trim().equals("")) {  
  103.             Toast.makeText(this, getString(R.string.pwd_check_empty),Toast.LENGTH_SHORT).show();  
  104.             return false;  
  105.         }  
  106.         return true;  
  107.     }  
  108. }  
package com.example.foolishfan.user_v10;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Resetpwd extends AppCompatActivity {
    private EditText mAccount;                        //用户名编辑
    private EditText mPwd_old;                        //密码编辑
    private EditText mPwd_new;                        //密码编辑
    private EditText mPwdCheck;                       //密码编辑
    private Button mSureButton;                       //确定按钮
    private Button mCancelButton;                     //取消按钮
    private UserDataManager mUserDataManager;         //用户数据管理类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resetpwd);
        mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);
        mPwd_old = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);
        mPwd_new = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);
        mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_check);

        mSureButton = (Button) findViewById(R.id.resetpwd_btn_sure);
        mCancelButton = (Button) findViewById(R.id.resetpwd_btn_cancel);

        mSureButton.setOnClickListener(m_resetpwd_Listener);      //注册界面两个按钮的监听事件
        mCancelButton.setOnClickListener(m_resetpwd_Listener);
        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }
    }
    View.OnClickListener m_resetpwd_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.resetpwd_btn_sure:                       //确认按钮的监听事件
                    resetpwd_check();
                    break;
                case R.id.resetpwd_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面
                    Intent intent_Resetpwd_to_Login = new Intent(Resetpwd.this,Login.class) ;    //切换Resetpwd Activity至Login Activity
                    startActivity(intent_Resetpwd_to_Login);
                    finish();
                    break;
            }
        }
    };
    public void resetpwd_check() {                                //确认按钮的监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd_old = mPwd_old.getText().toString().trim();
            String userPwd_new = mPwd_new.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd_old);
            if(result==1){                                             //返回1说明用户名和密码均正确,继续后续操作
                if(userPwd_new.equals(userPwdCheck)==false){           //两次密码输入不一样
                    Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                    return ;
                } else {
                    UserData mUser = new UserData(userName, userPwd_new);
                    mUserDataManager.openDataBase();
                    boolean flag = mUserDataManager.updateUserData(mUser);
                    if (flag == false) {
                        Toast.makeText(this, getString(R.string.resetpwd_fail),Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, getString(R.string.resetpwd_success),Toast.LENGTH_SHORT).show();
                        mUser.pwdresetFlag=1;
                        Intent intent_Register_to_Login = new Intent(Resetpwd.this,Login.class) ;    //切换User Activity至Login Activity
                        startActivity(intent_Register_to_Login);
                        finish();
                    }
                }
            }else if(result==0){                                       //返回0说明用户名和密码不匹配,重新输入
                Toast.makeText(this, getString(R.string.pwd_not_fit_user),Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }
    public boolean isUserNameAndPwdValid() {
        String userName = mAccount.getText().toString().trim();
        //检查用户是否存在
        int count=mUserDataManager.findUserByName(userName);
        //用户不存在时返回,给出提示文字
        if(count<=0){
            Toast.makeText(this, getString(R.string.name_not_exist, userName),Toast.LENGTH_SHORT).show();
            return false;
        }
        if (mAccount.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.account_empty),Toast.LENGTH_SHORT).show();
            return false;
        } else if (mPwd_old.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_empty),Toast.LENGTH_SHORT).show();
            return false;
        } else if (mPwd_new.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_new_empty),Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwdCheck.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_check_empty),Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
       User.java【用户个人中心】
  1. package com.example.foolishfan.user_v10;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class User extends AppCompatActivity {  
  10.     private Button mReturnButton;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.user);  
  15.         mReturnButton = (Button)findViewById(R.id.returnback);  
  16.     }  
  17.     public void back_to_login(View view) {  
  18.         Intent intent3 = new Intent(User.this,Login.class) ;  
  19.         startActivity(intent3);  
  20.         finish();  
  21.     }  
  22. }  
package com.example.foolishfan.user_v10;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class User extends AppCompatActivity {
    private Button mReturnButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);
        mReturnButton = (Button)findViewById(R.id.returnback);
    }
    public void back_to_login(View view) {
        Intent intent3 = new Intent(User.this,Login.class) ;
        startActivity(intent3);
        finish();
    }
}

二、功能分析

       上面工程涉及到的这些内容,在网上有很多写的很有参考价值的资料,建议可单独熟悉各个部分内容,然后添加功能到已有代码基础上,由于好多天后才整理的博客,缺少很多刚学习时遇到的种种错误及其解决方法,细节之处还望多交流。

<1>:Button,TextView,TextEdit,ImageView,RatioButton等基本控件的使用
<2>:多个Activities之间的切换
<3>:使用Android本地数据库来管理用户信息

1:登录功能

       通过登录按钮的监听事件实现登录,登录过程中需要判断用户名是否存在,密码是否正确,核心函数:
  1. public void login() {                        //登录按钮监听事件  
  2.     if (isUserNameAndPwdValid()) {  
  3.         String userName = mAccount.getText().toString().trim();//获取当前输入的用户名和密码信息  
  4.         String userPwd = mPwd.getText().toString().trim();  
  5.         SharedPreferences.Editor editor =login_sp.edit();  
  6.         int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);  
  7.         if(result==1){         //返回1说明用户名和密码均正确  
  8.             //保存用户名和密码  
  9.             editor.putString("USER_NAME", userName);  
  10.             editor.putString("PASSWORD", userPwd);  
  11.   
  12.             //是否记住密码  
  13.             if(mRememberCheck.isChecked()){  
  14.                 editor.putBoolean("mRememberCheck"true);  
  15.             }else{  
  16.                 editor.putBoolean("mRememberCheck"false);  
  17.             }  
  18.             editor.commit();  
  19.   
  20.             Intent intent = new Intent(Login.this,User.class) ;  
  21.             startActivity(intent);  
  22.             finish();  
  23.             Toast.makeText(this, getString(R.string.login_success),Toast.LENGTH_SHORT).show();//登录成功提示  
  24.         }else if(result==0){  
  25.             Toast.makeText(this, getString(R.string.login_fail),Toast.LENGTH_SHORT).show();  //登录失败提示  
  26.         }  
  27.     }  
  28. }  
    public void login() {                        //登录按钮监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();//获取当前输入的用户名和密码信息
            String userPwd = mPwd.getText().toString().trim();
            SharedPreferences.Editor editor =login_sp.edit();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
            if(result==1){         //返回1说明用户名和密码均正确
                //保存用户名和密码
                editor.putString("USER_NAME", userName);
                editor.putString("PASSWORD", userPwd);

                //是否记住密码
                if(mRememberCheck.isChecked()){
                    editor.putBoolean("mRememberCheck", true);
                }else{
                    editor.putBoolean("mRememberCheck", false);
                }
                editor.commit();

                Intent intent = new Intent(Login.this,User.class) ;
                startActivity(intent);
                finish();
                Toast.makeText(this, getString(R.string.login_success),Toast.LENGTH_SHORT).show();//登录成功提示
            }else if(result==0){
                Toast.makeText(this, getString(R.string.login_fail),Toast.LENGTH_SHORT).show();  //登录失败提示
            }
        }
    }

2:注册功能

       登录界面上点击注册按钮,进入对应监听事件,直接实现从Login的Activity到Register的Activity的跳转,然后在Register的Activity上实现用户注册,核心函数:

  1. public void register_check() {                                //确认按钮的监听事件  
  2.     if (isUserNameAndPwdValid()) {  
  3.         String userName = mAccount.getText().toString().trim();  
  4.         String userPwd = mPwd.getText().toString().trim();  
  5.         String userPwdCheck = mPwdCheck.getText().toString().trim();  
  6.         //检查用户是否存在  
  7.        int count=mUserDataManager.findUserByName(userName);  
  8.         //用户已经存在时返回,给出提示文字  
  9.         if(count>0){  
  10.             Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();  
  11.             return ;  
  12.         }  
  13.         if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样  
  14.             Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
  15.             return ;  
  16.         } else {  
  17.             UserData mUser = new UserData(userName, userPwd);  
  18.             mUserDataManager.openDataBase();  
  19.             long flag = mUserDataManager.insertUserData(mUser); //新建用户信息  
  20.             if (flag == -1) {  
  21.                 Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();  
  22.             }else{  
  23.                 Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();  
  24.                 Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    
  25.                 startActivity(intent_Register_to_Login);  
  26.                 finish();  
  27.             }  
  28.         }  
  29.     }  
  30. }  
    public void register_check() {                                //确认按钮的监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd = mPwd.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            //检查用户是否存在
           int count=mUserDataManager.findUserByName(userName);
            //用户已经存在时返回,给出提示文字
            if(count>0){
                Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();
                return ;
            }
            if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样
                Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                return ;
            } else {
                UserData mUser = new UserData(userName, userPwd);
                mUserDataManager.openDataBase();
                long flag = mUserDataManager.insertUserData(mUser); //新建用户信息
                if (flag == -1) {
                    Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;  
                    startActivity(intent_Register_to_Login);
                    finish();
                }
            }
        }
    }

3:修改密码功能

      在登录界面点击“修改密码”即可进入对应的Resetpwd的Activity,实现用户密码的修改,核心函数:

  1. public void resetpwd_check() {                                //确认按钮的监听事件  
  2.     if (isUserNameAndPwdValid()) {  
  3.         String userName = mAccount.getText().toString().trim();  
  4.         String userPwd_old = mPwd_old.getText().toString().trim();  
  5.         String userPwd_new = mPwd_new.getText().toString().trim();  
  6.         String userPwdCheck = mPwdCheck.getText().toString().trim();  
  7.         int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd_old);  
  8.         if(result==1){                                             //返回1说明用户名和密码均正确,继续后续操作  
  9.             if(userPwd_new.equals(userPwdCheck)==false){           //两次密码输入不一样  
  10.                 Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
  11.                 return ;  
  12.             } else {  
  13.                 UserData mUser = new UserData(userName, userPwd_new);  
  14.                 mUserDataManager.openDataBase();  
  15.                 boolean flag = mUserDataManager.updateUserData(mUser);  
  16.                 if (flag == false) {  
  17.                     Toast.makeText(this, getString(R.string.resetpwd_fail),Toast.LENGTH_SHORT).show();  
  18.                 }else{  
  19.                     Toast.makeText(this, getString(R.string.resetpwd_success),Toast.LENGTH_SHORT).show();  
  20.                     mUser.pwdresetFlag=1;  
  21.                     Intent intent_Register_to_Login = new Intent(Resetpwd.this,Login.class) ;      
  22.                     startActivity(intent_Register_to_Login);  
  23.                     finish();  
  24.                 }  
  25.             }  
  26.         }else if(result==0){                                       //返回0说明用户名和密码不匹配,重新输入  
  27.             Toast.makeText(this, getString(R.string.pwd_not_fit_user),Toast.LENGTH_SHORT).show();  
  28.             return;  
  29.         }  
  30.     }  
  31. }  
    public void resetpwd_check() {                                //确认按钮的监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd_old = mPwd_old.getText().toString().trim();
            String userPwd_new = mPwd_new.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd_old);
            if(result==1){                                             //返回1说明用户名和密码均正确,继续后续操作
                if(userPwd_new.equals(userPwdCheck)==false){           //两次密码输入不一样
                    Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                    return ;
                } else {
                    UserData mUser = new UserData(userName, userPwd_new);
                    mUserDataManager.openDataBase();
                    boolean flag = mUserDataManager.updateUserData(mUser);
                    if (flag == false) {
                        Toast.makeText(this, getString(R.string.resetpwd_fail),Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, getString(R.string.resetpwd_success),Toast.LENGTH_SHORT).show();
                        mUser.pwdresetFlag=1;
                        Intent intent_Register_to_Login = new Intent(Resetpwd.this,Login.class) ;    
                        startActivity(intent_Register_to_Login);
                        finish();
                    }
                }
            }else if(result==0){                                       //返回0说明用户名和密码不匹配,重新输入
                Toast.makeText(this, getString(R.string.pwd_not_fit_user),Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

4:注销功能

       注销功能直接在登录界面,当用户名和密码均正确时,点击注销按钮将直接注销用户【各位可根据需求将注销放在个人中心里面进行操作】。核心函数:

  1. public void cancel() {           //注销  
  2.     if (isUserNameAndPwdValid()) {  
  3.         String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息  
  4.         String userPwd = mPwd.getText().toString().trim();  
  5.         int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);  
  6.         if(result==1){                                             //返回1说明用户名和密码均正确  
  7.             Toast.makeText(this, getString(R.string.cancel_success),Toast.LENGTH_SHORT).show();//登录成功提示  
  8.             mPwd.setText("");  
  9.             mAccount.setText("");  
  10.             mUserDataManager.deleteUserDatabyname(userName);  
  11.         }else if(result==0){  
  12.             Toast.makeText(this, getString(R.string.cancel_fail),Toast.LENGTH_SHORT).show();  //登录失败提示  
  13.         }  
  14.     }  
  15.   
  16. }  
    public void cancel() {           //注销
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();    //获取当前输入的用户名和密码信息
            String userPwd = mPwd.getText().toString().trim();
            int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
            if(result==1){                                             //返回1说明用户名和密码均正确
                Toast.makeText(this, getString(R.string.cancel_success),Toast.LENGTH_SHORT).show();//登录成功提示
                mPwd.setText("");
                mAccount.setText("");
                mUserDataManager.deleteUserDatabyname(userName);
            }else if(result==0){
                Toast.makeText(this, getString(R.string.cancel_fail),Toast.LENGTH_SHORT).show();  //登录失败提示
            }
        }

    }

5:记住密码

       当记住密码的RadioButton为真时,系统将记住此时用户登录的信息,用户在下次登录时可避免再次输入用户名和密码的麻烦,实现细节在Login的Activity中。这里给一个大牛用SharedPreferences做的记住密码连接:这里 


三、工程源码下载

       
        初学不久,工程代码中可能存在一些bug,望各位不吝指出,有个bug测试出来了,就是在记住密码之后对该账户进行密码修改后返回登录界面,此时记住的密码为原来的密码,不可登陆,我相信这个小bug是难不到你们的,^_^。
        例程源码User_V2.0

From:http://blog.csdn.net/stayfoolish_fan/article/details/51933563
展开阅读全文

没有更多推荐了,返回首页