简易UI
用户登录注册界面开发及用户信息管理案例详解
刚开始接触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编程,这算是我写的第一个简单工程,主要功能有:用户登录、注册、注销、修改密码、记住密码共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【登录界面】
<?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>
<?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【注册界面】
<?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>
<?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【修改密码界面】
<?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>
<?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【个人中心】
<?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>
<?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 【用户数据】
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;
}
}
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【用户管理】
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;
}
}
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】
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();
}
}
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】
[java] view plain copy
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;
}
}
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【重新设置密码】
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;
}
}
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【用户个人中心】
[java] view plain copy
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();
}
}
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:登录功能
通过登录按钮的监听事件实现登录,登录过程中需要判断用户名是否存在,密码是否正确,核心函数:
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(); //登录失败提示
}
}
}
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上实现用户注册,核心函数:
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();
}
}
}
}
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,实现用户密码的修改,核心函数:
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;
}
}
}
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:注销功能
注销功能直接在登录界面,当用户名和密码均正确时,点击注销按钮将直接注销用户【各位可根据需求将注销放在个人中心里面进行操作】。核心函数:
[java] view plain copy
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(); //登录失败提示
}
}
}
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中。
LoginActivity.java
package com.liu.activity;
import android.app.Activity;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText userName, password;
private CheckBox rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
//获得实例对象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
password = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_mima);
auto_login = (CheckBox) findViewById(R.id.cb_auto);
btn_login = (Button) findViewById(R.id.btn_login);
btnQuit = (ImageButton)findViewById(R.id.img_btn);
//判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false))
{
//设置默认是记录密码状态
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//设置默认是自动登录状态
auto_login.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 登录监听事件 现在默认为用户名为:liu 密码:123
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
if(userNameValue.equals("liu")&&passwordValue.equals("123"))
{
Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
//登录成功和记住密码框为选中状态才保存用户信息
if(rem_pw.isChecked())
{
//记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
});
//监听记住密码多选框按钮事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {
System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//监听自动登录多选框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (auto_login.isChecked()) {
System.out.println("自动登录已选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
System.out.println("自动登录没有选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
btnQuit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
LogoActivity.java
package com.liu.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class LogoActivity extends Activity {
private ProgressBar progressBar;
private Button backButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.logo);
progressBar = (ProgressBar) findViewById(R.id.pgBar);
backButton = (Button) findViewById(R.id.btn_back);
Intent intent = new Intent(this, WelcomeAvtivity.class);
LogoActivity.this.startActivity(intent);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
本文地址:https://blog.csdn.net/z031022/article/details/107166198
下一篇: 静态页和Memcached的区别?