实现注册、登录、注销账户
MainActivity.java
package cn.lixyz.activity; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import cn.lixyz.data.MySQLiteOpenHelper; import cn.lixyz.sqlite.R; public class MainActivity extends Activity { // 声明组件 private EditText et_login_username, et_login_password; private Button bt_login_login, bt_login_register; private CheckBox cb_login_rember; private TextView tv_login_forget; // 声明一个意图 private Intent intent; // 声明SQLite相关内容 private MySQLiteOpenHelper msop; private SQLiteDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到组件 findView(); // 创建数据库 msop = new MySQLiteOpenHelper(this, "user.db", null, 1); // 创建或打开一个数据库有两种方法,getWritableDatabase()和getReadableDatabase(),二者的区别是: // getWritableDatabase取得的实例是以读写的方式打开数据库,如果打开的数据库磁盘满了,此时只能读不能写,此时调用了getWritableDatabase的实例,那么将会发生错误(异常) // getReadableDatabase取得的实例是先调用getWritableDatabase以读写的方式打开数据库,如果数据库的磁盘满了,此时返回打开失败,继而用getReadableDatabase的实例以只读的方式去打开数据库 database = msop.getReadableDatabase(); } // 将findViewById独立封装,以清晰代码结构 private void findView() { et_login_username = (EditText) findViewById(R.id.et_login_username); et_login_password = (EditText) findViewById(R.id.et_login_password); bt_login_login = (Button) findViewById(R.id.bt_login_login); bt_login_register = (Button) findViewById(R.id.bt_login_register); cb_login_rember = (CheckBox) findViewById(R.id.cb_login_rember); tv_login_forget = (TextView) findViewById(R.id.tv_login_forget); } // 通过ID来判断点击的是哪个按钮 public void clickButton(View view) { switch (view.getId()) { case R.id.bt_login_login: login(); break; case R.id.bt_login_register: register(); break; case R.id.tv_login_forget: forgetPassword(); break; } } // 登录方法 private void login() { String username = et_login_username.getText().toString().trim(); String password = et_login_password.getText().toString().trim(); Cursor c = database.rawQuery("select * from user where username=? and password=?", new String[] { username, password }); if (c.moveToFirst()) { intent = new Intent(MainActivity.this, IndexActivity.class); startActivity(intent); finish(); } else { Toast.makeText(MainActivity.this, "登录不成功,检查您的账户和密码", Toast.LENGTH_SHORT).show(); } c.close(); } // 注册方法 private void register() { intent = new Intent(MainActivity.this, RegesiterActivity.class); startActivity(intent); } // 找回密码方法 private void forgetPassword() { intent = new Intent(MainActivity.this, ForgetPasswordActivity.class); startActivity(intent); } }
activity_main.xml
<!-- 首页布局 --> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <!-- 一个ImageView控件放置logo --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="15dp" android:src="@drawable/logo" /> <!-- 两个EditText,输入用户名和密码 --> <EditText android:id="@+id/et_login_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的用户名" /> <EditText android:id="@+id/et_login_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的密码" android:password="true" /> <!-- 一个水平的线性布局,用来放置登录和注册按钮 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" > <Button android:id="@+id/bt_login_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="clickButton" android:text="登录" /> <Button android:id="@+id/bt_login_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="clickButton" android:text="注册" /> </LinearLayout> <!-- 一个水平的线性布局,用来放置记住用户名和忘记密码 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_login_rember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="记住用户名" /> <!-- 将weight设置为1,全占剩余空间,以保障忘记密码水平居右 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_login_forget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="clickButton" android:text="忘记密码 " /> </LinearLayout> </LinearLayout>
RegesiterActivity.java
package cn.lixyz.activity; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import cn.lixyz.data.MySQLiteOpenHelper; import cn.lixyz.sqlite.R; public class RegesiterActivity extends Activity { // 声明组件 private EditText et_reg_username, et_reg_password, et_reg_againPassword, et_reg_mobilephone; private Button bt_reg_submit; // 声明要插入的数据变量 private String username, password, mobilephone; // 声明数据库组件 private MySQLiteOpenHelper msoh; private SQLiteDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); findView(); msoh = new MySQLiteOpenHelper(RegesiterActivity.this, "user.db", null, 1); database = msoh.getReadableDatabase(); bt_reg_submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String str = et_reg_againPassword.getText().toString().trim(); String str2 = et_reg_password.getText().toString().trim(); // 判断密码是否输入一致,如果不一致,弹出提示,并清空输入的密码 if (str.equals(str2)) { password = str; } else { Toast.makeText(RegesiterActivity.this, "您输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show(); et_reg_password.setText(""); et_reg_againPassword.setText(""); return; } username = et_reg_username.getText().toString().trim(); mobilephone = et_reg_mobilephone.getText().toString().trim(); // 打包插入数据 ContentValues cv = new ContentValues(); cv.put("username", username); cv.put("password", password); cv.put("mobilephone", mobilephone); if (database.insert("user", null, cv) > 0) { Toast.makeText(RegesiterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(RegesiterActivity.this, "注册不成功", Toast.LENGTH_SHORT).show(); } } }); } //// 将findViewById独立封装,以清晰代码结构 private void findView() { et_reg_username = (EditText) findViewById(R.id.et_reg_username); et_reg_password = (EditText) findViewById(R.id.et_reg_password); et_reg_againPassword = (EditText) findViewById(R.id.et_reg_againPassword); et_reg_mobilephone = (EditText) findViewById(R.id.et_reg_mobilephone); bt_reg_submit = (Button) findViewById(R.id.bt_reg_submit); } }
activity_register.xml
<!-- 注册布局 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 注册用户名 --> <EditText android:id="@+id/et_reg_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您要注册的用户名" /> <!-- 注册密码 --> <EditText android:id="@+id/et_reg_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您要注册的密码" android:password="true" /> <!-- 重复注册密码 --> <EditText android:id="@+id/et_reg_againPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="再次输入密码" android:password="true" /> <!-- 注册手机号 --> <EditText android:id="@+id/et_reg_mobilephone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的手机号" /> <!-- 提交注册按钮 --> <Button android:id="@+id/bt_reg_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="提 交" /> </LinearLayout>
IndexActivity.java
package cn.lixyz.activity; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import cn.lixyz.data.MySQLiteOpenHelper; import cn.lixyz.sqlite.R; public class IndexActivity extends Activity { private EditText et_del_mobile; private Button bt_del_submit; private MySQLiteOpenHelper msop; private SQLiteDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); findView(); msop = new MySQLiteOpenHelper(IndexActivity.this, "user.db", null, 1); database = msop.getReadableDatabase(); bt_del_submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String mobile = et_del_mobile.getText().toString().trim(); Cursor c = database.rawQuery("select mobilephone from user where mobilephone=?", new String[] { mobile }); if (c.moveToFirst()) { int i = database.delete("user", "mobilephone=?", new String[] { mobile }); if (i > 0) { Toast.makeText(IndexActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(IndexActivity.this, "*删除不成功*", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(IndexActivity.this, "删除不成功", Toast.LENGTH_SHORT).show(); } } }); } private void findView() { et_del_mobile = (EditText) findViewById(R.id.et_del_mobile); bt_del_submit = (Button) findViewById(R.id.bt_del_submit); } }
activity_index.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注销账户" /> <EditText android:id="@+id/et_del_mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="验证您的手机号" /> <Button android:id="@+id/bt_del_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点我验证,删除账户" /> </LinearLayout>
ForgetPasswordActivity.java
package cn.lixyz.activity; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import cn.lixyz.data.MySQLiteOpenHelper; import cn.lixyz.sqlite.R; public class ForgetPasswordActivity extends Activity { private EditText et_forget_mobile, et_forget_username, et_forget_password, et_forget_againPassword; private Button bt_forget_submit; private MySQLiteOpenHelper msop; private SQLiteDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_forget); findView(); msop = new MySQLiteOpenHelper(this, "user.db", null, 1); database = msop.getReadableDatabase(); bt_forget_submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String username = et_forget_username.getText().toString().trim(); String mobile = et_forget_mobile.getText().toString().trim(); Cursor c = database.rawQuery("select username,mobilephone from user where username=? and mobilephone=?", new String[] { username, mobile }); if (c.moveToFirst()) { String str = et_forget_password.getText().toString().trim(); String str2 = et_forget_againPassword.getText().toString().trim(); if (str.equals(str2)) { ContentValues cv = new ContentValues(); cv.put("password", str); database.update("user", cv, "username=? and mobilephone=?", new String[] { username, mobile }); Toast.makeText(ForgetPasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); finish(); } } } }); } private void findView() { et_forget_mobile = (EditText) findViewById(R.id.et_forget_mobile); et_forget_username = (EditText) findViewById(R.id.et_forget_username); et_forget_password = (EditText) findViewById(R.id.et_forget_password); et_forget_againPassword = (EditText) findViewById(R.id.et_forget_againPassword); bt_forget_submit = (Button) findViewById(R.id.bt_forget_submit); } }
activity_forget.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_forget_mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的手机号" /> <EditText android:id="@+id/et_forget_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的用户名" /> <EditText android:id="@+id/et_forget_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="输入您的密码" /> <EditText android:id="@+id/et_forget_againPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="重复输入您的密码" /> <Button android:id="@+id/bt_forget_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="修改" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.lixyz.sqlite" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cn.lixyz.activity.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cn.lixyz.activity.RegesiterActivity" /> <activity android:name="cn.lixyz.activity.IndexActivity" /> <activity android:name="cn.lixyz.activity.ForgetPasswordActivity" /> </application> </manifest>
时间: 2024-10-07 05:25:28