<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hhh.com.can"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="点餐管理系统" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name=".find"> </activity> <activity android:name=".login"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".register"> </activity> <activity android:name=".add"> </activity> <activity android:name=".alt"> </activity> </application> </manifest>
package hhh.com.can; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class register extends AppCompatActivity implements View.OnClickListener { private Button bt_register; private EditText et_ID,et_password; private String username,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); et_ID = (EditText) findViewById(R.id.et_ID); et_password = (EditText) findViewById(R.id.et_password); bt_register = (Button) findViewById(R.id.bt_register); bt_register.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt_register: username = et_ID.getText().toString().trim(); password = et_password.getText().toString().trim(); if(username.equals("") || password.equals("")){ Toast.makeText(this,"请完善信息!",Toast.LENGTH_SHORT).show(); return; }else { userService userService = new userService(this); Boolean flag = userService.CheckIsDataAlreadyInDBorNot(username); if(flag==Boolean.TRUE){ Toast.makeText(this,"该用户已存在!",Toast.LENGTH_SHORT).show(); return; }else { user user = new user(); user.setUsername(username); user.setPassword(password); userService.register(user); Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show(); finish(); } } break; } } }
package hhh.com.can; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * 用户登录注册处理类 */ public class userService { private DBHelper dbHelper; public userService(Context context){ dbHelper=new DBHelper(context); } //登录用,检测账号密码是否与数据库中的信息匹配 public boolean login(String username,String password){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select * from user where username=? and password=?"; Cursor cursor=sdb.rawQuery(sql, new String[]{username,password}); if(cursor.moveToFirst()==true){ cursor.close(); return true; } return false; } //注册用,将相应的账号密码写入数据库 public boolean register(user user){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="insert into user(username,password) values(?,?)"; Object obj[]={user.getUsername(),user.getPassword()}; sdb.execSQL(sql, obj); return true; } //检测该账号的用户是否存在 public boolean CheckIsDataAlreadyInDBorNot(String value) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String Query = "select * from user where username =?"; Cursor cursor = db.rawQuery(Query, new String[]{value}); if (cursor.getCount() > 0) { cursor.close(); return true; } cursor.close(); return false; } }