Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 【Android】用戶登錄注冊界面開發及用戶信息管理案例代碼詳解

【Android】用戶登錄注冊界面開發及用戶信息管理案例代碼詳解

編輯:關於Android編程

用戶登錄注冊界面開發及用戶信息管理案例詳解

剛開始接觸Android編程,這算是我寫的第一個簡單工程,主要功能有:用戶登錄、注冊、注銷、修改密碼、記住密碼共5個基本操作,其內容涉及到以下幾點:

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

2:多個Activities之間的切換;

3:使用Android本地數據庫來管理用戶信息。

先上幾個圖【我在android6.0.1的真機上調試的】:

下面是用戶登錄和用戶注冊界面。

 

 

\

下面是用戶密碼修改和個人中心界面。

\

一、工程代碼

整個工程是在Android Studio 2.1.1環境下實現的,源碼下載鏈接在末尾給出,下面是工程目錄: \ 這裡先貼出各個布局文件.xml,各個類內容和各個activity代碼:

1:幾個界面布局文件.xml

login.xml【登錄界面】


    
register.xml【注冊界面】



    

    

    
resetpwd.xml【修改密碼界面】



    

    

    
user.xml【個人中心】



    

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;
    }

}
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;
    }

}

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();//注銷成功提示
                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】
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;
    }
}
User.java【用戶個人中心】
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();  //登錄失敗提示
            }
        }
    }

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();
                }
            }
        }
    }

 

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;
            }
        }
    }

 

4:注銷功能

注銷功能直接在登錄界面,當用戶名和密碼均正確時,點擊注銷按鈕將直接注銷用戶【各位可根據需求將注銷放在個人中心裡面進行操作】。核心函數:

 

    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中。

 

三、工程源碼下載

  初學不久,工程代碼中可能存在一些bug,望各位不吝指出,有個bug測試出來了,就是在記住密碼之後對該賬戶進行密碼修改後返回登錄界面,此時記住的密碼為原來的密碼,不可登陸,我相信這個小bug是難不到你們的,^_^。 例程源碼User_V2.0
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved