每日总结之app的登录

重点:activity之间的跳转,以及sqliteopenhelper的使用

自定义一个mysqliteeopenhelper类继承与静态的sqliteopenhelper;

package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.myapplication.javabean.User;

public class Mysqliteopenhelper extends SQLiteOpenHelper {

    private  static final String DB_NAME="Mysqlite.db";

    private  static final String creat_users="create table users(name varchar(32),password varchar(32))";
    public Mysqliteopenhelper(@Nullable Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(creat_users);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long  Register(User u) {
        /*
        getReadableDatabase()方法则是先以读写方式打开数据库
        如果数据库的磁盘空间满了,就会打开失败
        当打开失败后会继续尝试以只读方式打开数据库。
        如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。
         */
        SQLiteDatabase db=  getReadableDatabase();
        //将参数name和password放入 ContentValues的实例化对象里面
        ContentValues cv=new ContentValues();
        cv.put("name",u.getName());
        cv.put("password",u.getPassword());
        //将这个对象插入到数据库
        long users=db.insert("users",null,cv);
        return users;
    }
    public  boolean login(String name,String  password){
        SQLiteDatabase db1=  getReadableDatabase();
        boolean result=false;
        /*
        遍历表里面的每一行(按行查找)
        看是否有与输入姓名一样的字段"name like ?"
        若有继续查找密码
        没有则查无此人

         */
       Cursor cursor= db1.query("users",null,"name like ?",new String[]{name},null,null,null);
       if(cursor!=null){
           //游标锁定一行后查找该行的密码列,用getString()方法获取密码
           while (cursor.moveToNext()){
               String password1 = cursor.getString(1);
               //比较找到的密码是否和输入密码一致,返回0/1
               result=password1.equals(password);
               return result;
           }
       }
return false;
    }
}

主页面代码:获取不同的按钮id值,文本输入值,通过Switch——case选择不同按钮调用功能函数(增删改查)的功能,可以通过main.xml文件对按钮添加点击监听事件,也可以添加onCilck添加对应函数

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button login,register;
    private EditText name,password;
    private Mysqliteopenhelper mysqliteopenhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mysqliteopenhelper=new Mysqliteopenhelper(this);
        find();

    }

    private void find() {
        login=findViewById(R.id.login);
        register=findViewById(R.id.register);
        name=findViewById(R.id.ename);
        password=findViewById(R.id.epassword);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.login:
                String s=name.getText().toString();
                String ss=password.getText().toString();
              boolean login=  mysqliteopenhelper.login(s,ss);
                if (login){
                    Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this,good.class);
                    startActivity(intent);
                }else
                {
                    Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.register:
                Intent intent1= new Intent(this,register.class);
                startActivity(intent1);
                break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">

   <View
       android:layout_width="match_parent"
       android:layout_height="120dp" />
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="80dp"
      android:orientation="horizontal">
      <TextView
          android:layout_width="0dp"
          android:layout_height="match_parent"
         android:layout_weight="1"
         android:text="账号"
         android:id="@+id/name"
         android:textSize="30dp"
         android:gravity="center_vertical"/>
   <EditText
       android:id="@+id/ename"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="3"
       android:hint="请输入用户名"/>
   </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="80dp"
       android:orientation="horizontal">
      <TextView
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="密码"
          android:id="@+id/password"
          android:textSize="30dp"
          android:gravity="center_vertical"/>
      <EditText
          android:id="@+id/epassword"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="3"
          android:hint="请输入用密码"/>
   </LinearLayout>
<Button
    android:layout_width="160dp"
    android:layout_height="80dp"
   android:text="登录"
   android:textSize="30dp"
    android:background="@color/colorAccent"
   android:layout_gravity="center"
   android:id="@+id/login"/>

   <Button
       android:layout_width="160dp"
       android:layout_height="80dp"
       android:text="注册"
       android:textSize="30dp"
       android:background="@color/colorAccent"
       android:layout_gravity="center"
       android:id="@+id/register"/>
</LinearLayout>

注册活动:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.javabean.User;

public class register extends AppCompatActivity {
    private Button register1;
    private EditText name1,password1;
    private Mysqliteopenhelper mysqliteopenhelper1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mysqliteopenhelper1=new Mysqliteopenhelper(this);
        find();
    }

    private void find() {

        register1=findViewById(R.id.register1);
        name1=findViewById(R.id.ename1);
        password1=findViewById(R.id.epassword1);

    }

    public void zhuce(View view) {
        String s=name1.getText().toString();
        String ss=password1.getText().toString();
        User user = new User(s,ss);
        long l=mysqliteopenhelper1.Register(user);
        if(l!=-1){
            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
            Intent intent3=new Intent(this,MainActivity.class);
            startActivity(intent3);

        }else{
            Toast.makeText(this,"注册失败",Toast.LENGTH_SHORT).show();
        }

    }
}

 

posted @ 2023-02-26 21:14  cojames  阅读(46)  评论(0编辑  收藏  举报