布局login
xml version= "1.0" encoding= "utf-8" ?>
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
>
android :id= "@+id/login_view"
android :layout_width= "400dp"
android :layout_height= "800dp"
android :layout_centerInParent= "true"
>
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" />
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" />
android :layout_width= "300dp"
android :layout_height= "150dp"
android :id= "@+id/logo"
android :src= "@mipmap/ic_launcher"
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" />
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" />
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" />
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" />
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" />
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" />
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" >
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" />
布局 register
xml version= "1.0" encoding= "utf-8" ?>
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :weightSum= "1" >
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" />
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" />
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" />
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= " 请确认您的密码 " />
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" />
布局 regisetpwd xml version= "1.0" encoding= "utf-8" ?>
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :weightSum= "1" >
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" />
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" />
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" />
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= " 请确认您的新密码 "
/>
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" />
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" />
布局 userxml version= "1.0" encoding= "utf-8" ?>
xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :orientation= "vertical" >
android :id= "@+id/rl"
android :layout_width= "match_parent"
android :layout_height= "wrap_content" >
android :id= "@+id/main_geren"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentTop= "true"
android :layout_centerHorizontal= "true"
android :text= " 个人信息 "
android :textColor= "#000"
android :textSize= "40sp" />
android :id= "@+id/view"
android :layout_width= "match_parent"
android :layout_height= "2dp"
android :layout_below= "@+id/rl"
android :background= "#000" />
android :id= "@+id/rl1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :padding= "16dp" >
android :id= "@+id/textView2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_marginTop= "10dp"
android :text= " 头像 "
android :textColor= "#000"
android :textSize= "20sp" />
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentEnd= "true"
android :layout_alignParentRight= "true"
android :layout_alignParentTop= "true"
android :layout_marginEnd= "42dp"
android :layout_marginRight= "42dp"
android :src= "@mipmap/ic_launcher" />
android :id= "@+id/textView"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignBaseline= "@+id/textView2"
android :layout_alignBottom= "@+id/textView2"
android :layout_alignParentEnd= "true"
android :layout_alignParentRight= "true"
android :layout_marginRight= "10dp"
android :text= ">"
android :textColor= "#000"
android :textSize= "30sp" />
android :layout_width= "match_parent"
android :layout_height= "2dp"
android :layout_below= "@+id/rl1"
android :background= "#000" />
android :id= "@+id/rl2"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :padding= "16dp" >
android :id= "@+id/main_textview_touxiang"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_marginTop= "10dp"
android :text= " 用户名 "
android :textColor= "#000"
android :textSize= "20sp" />
android :id= "@+id/user_textView_user"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignBaseline= "@+id/main_textview_touxiang"
android :layout_alignBottom= "@+id/main_textview_touxiang"
android :layout_alignParentEnd= "true"
android :layout_alignParentRight= "true"
android :layout_marginRight= "10dp"
android :text= " 用户 "
android :textColor= "#000"
android :textSize= "30sp" />
android :layout_width= "match_parent"
android :layout_height= "2dp"
android :layout_below= "@+id/rl2"
android :background= "#000" />
android :id= "@+id/rl3"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :padding= "16dp" >
android :id= "@+id/main_textView_nicheng"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_marginTop= "10dp"
android :text= " 昵称 "
android :textColor= "#000"
android :textSize= "20sp" />
android :id= "@+id/naim_textView_name"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignBottom= "@+id/main_textView_nicheng"
android :layout_alignParentEnd= "true"
android :layout_alignParentRight= "true"
android :text= " 未登录 "
android :textColor= "#000"
android :textSize= "30sp" />
android :layout_width= "match_parent"
android :layout_height= "2dp"
android :layout_below= "@+id/rl3"
android :background= "#000" />
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :padding= "10dp" >
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_alignParentLeft= "true"
android :layout_alignParentStart= "true"
android :layout_centerVertical= "true"
android :layout_marginLeft= "10dp"
android :layout_marginStart= "10dp" >
android :id= "@+id/main_button1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_weight= "1"
android :onClick= "back_to_login"
android :text= " 退出登录 " />
android :id= "@+id/returnback"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_weight= "1"
android :onClick= " shangpin "
android :text= " 跳转商品搜索页面 " />
代码 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.mipmap.ic_launcher );
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
intent.putExtra("name" , userName);
startActivity(intent);
finish();
Toast.makeText (this, " 登陆成功!! " , Toast.LENGTH_SHORT ).show();
}else if (result==0 ){
Toast.makeText (this, " 账号未注册!! " , 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, " 注销成功!! " , Toast.LENGTH_SHORT ).show();
mPwd .setText("" );
mAccount .setText("" );
mUserDataManager .deleteUserDatabyname(userName);
}else if (result==0 ){
Toast.makeText (this, " 注销失败!! " , Toast.LENGTH_SHORT ).show();
}
}
}
public boolean isUserNameAndPwdValid () {
if (mAccount .getText().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入账号!! " , Toast.LENGTH_SHORT ).show();
return false;
} else if (mPwd .getText().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入密码!! " , 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();
}
} import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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, " 用户已存在!! " , Toast.LENGTH_SHORT ).show();
return ;
}
if (userPwd.equals(userPwdCheck)==false ){ // 两次密码输入不一样
Toast.makeText (this, " 密码不一致!! " , 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, "11 !! " , Toast.LENGTH_SHORT ).show();
}else {
Toast.makeText (this, " 注册成功!! " , 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, " 请输入账号!! " , Toast.LENGTH_SHORT ).show();
return false;
} else if (mPwd .getText().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入密码!! " , Toast.LENGTH_SHORT ).show();
return false;
}else if (mPwdCheck .getText().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入确认密码!! " , Toast.LENGTH_SHORT ).show();
return false;
}
return true;
}
}
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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, "a !! " , 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, "b !! " , Toast.LENGTH_SHORT ).show();
}else {
Toast.makeText (this, "c !! " , 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, "d !! " , 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, "q !! " , Toast.LENGTH_SHORT ).show();
return false;
}
if (mAccount .getText ().toString().trim().equals("" )) {
Toast.makeText (this, "w !! " , Toast.LENGTH_SHORT ).show();
return false;
} else if (mPwd_old .getText ().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入旧密码!! " , Toast.LENGTH_SHORT ).show();
return false;
} else if (mPwd_new .getText ().toString().trim().equals("" )) {
Toast.makeText (this, " 请输入新密码!! " , Toast.LENGTH_SHORT ).show();
return false;
}else if (mPwdCheck .getText ().toString().trim().equals("" )) {
Toast.makeText (this, " 请确认新密码!! " , Toast.LENGTH_SHORT ).show();
return false;
}
return true;
}
} import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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 );
String name = getIntent().getStringExtra("name" );
TextView tvuser = (TextView) findViewById(R.id.user_textView_user );
TextView tvname = (TextView) findViewById(R.id.naim_textView_name );
tvname.setText("kson" );
tvuser.setText(name);
}
public void back_to_login (View view) {
Intent intent3 = new Intent(User.this, Login.class );
startActivity(intent3);
finish();
}
public void shangpin (View view) {
Intent intent3 = new Intent(User.this, MainActivity.class );
startActivity(intent3);
finish();
}
} 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;
}
} 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;
}
}