編輯:關於Android編程
用戶登錄注冊界面開發及用戶信息管理案例詳解
剛開始接觸Android編程,這算是我寫的第一個簡單工程,主要功能有:用戶登錄、注冊、注銷、修改密碼、記住密碼共5個基本操作,其內容涉及到以下幾點:
1:Button,TextView,TextEdit,ImageView,RatioButton等基本控件的使用;
2:多個Activities之間的切換;
3:使用Android本地數據庫來管理用戶信息。
先上幾個圖【我在android6.0.1的真機上調試的】:
下面是用戶登錄和用戶注冊界面。
下面是用戶密碼修改和個人中心界面。
register.xml【注冊界面】
resetpwd.xml【修改密碼界面】
user.xml【個人中心】
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.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本地數據庫來管理用戶信息
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(); //登錄失敗提示 } } }
登錄界面上點擊注冊按鈕,進入對應監聽事件,直接實現從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(); } } } }
在登錄界面點擊“修改密碼”即可進入對應的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 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(); //登錄失敗提示 } } }
當記住密碼的RadioButton為真時,系統將記住此時用戶登錄的信息,用戶在下次登錄時可避免再次輸入用戶名和密碼的麻煩,實現細節在Login的Activity中。
照例先上效果圖通過該例子,你能學到什麼: 對Paint 深入理解,畫繪制餅圖,矩形,文字等 &nbs
ExpandableListView簡介ExpandableListView是ListView的子類,用法和ListView類似,ExpandableListView可以
我們使用社交軟件的過程中多多少少會為別人的帖子點贊,如圖 : 可以看到用戶頁面顯示出來的只是點了贊的用戶的名稱,點擊這些名稱可以進入到該用戶的主頁。我們就
陌陌手機版賬號部分功能受限如何處理?若您的陌陌號已綁定手機,且綁定手機可以使用,請參考以下步驟解除功能受限:1、通過幫助頁面進入“賬號狀態&rd