今天学习了登陆界面的操作。
学习了连接数据库的用户登录:
package com.example.clock.db; 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.clock.jiben.User; import com.example.clock.jiben.User; import java.util.ArrayList; public class DatabaseHelper extends SQLiteOpenHelper { //创建一个数据库 private SQLiteDatabase db; public DatabaseHelper(@Nullable Context context) { super(context, "db_test", null, 1); db = getReadableDatabase(); } @Override public void onCreate(SQLiteDatabase db) { //在第一次创建数据库的时候,创建一些字段 String sql = "create table user(_id integer, name varchar(50), password varchar(40))"; db.execSQL(sql);//sql语句的执行函数 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //如果这个表中存在user,我们可以先把他去掉,然后重新创建 String sql = "DROP TABLE IF EXISTS user"; db.execSQL(sql); onCreate(db); } //为使项目结构更加紧凑,我们在此类中编写增删改查的函数,因为只有登录和注册界面,因此只涉及到写入数据库insert和query的操作 public void insert(String name,String password ){ db.execSQL("insert into user(name,password)VALUES(?,?)",new Object[]{name,password}); } public ArrayList<User> getAllDATA(){//查询数据库 ArrayList<User> list = new ArrayList<User>(); //查询数据库中的数据,并将这些数据按照降序的情况排列 Cursor cursor = db.query("user",null,null,null,null,null,"name DESC"); while(cursor.moveToNext()){ int index_name = cursor.getColumnIndex("name"); int index_password = cursor.getColumnIndex("password"); String name = cursor.getString(index_name); String password = cursor.getString(index_password); list.add(new User(name,password)); } return list; } }
package com.example.clock; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.clock.db.DatabaseHelper; import com.example.clock.jiben.User; import com.example.clock.jiben.User; import java.util.ArrayList; public class Login extends AppCompatActivity { private DatabaseHelper mSQLite; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Button btn_login = findViewById(R.id.login); Button btn_register = findViewById(R.id.register); EditText ed_name = findViewById(R.id.userName); EditText ed_password = findViewById(R.id.userpassword); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = ed_name.getText().toString().trim(); String password = ed_password.getText().toString().trim(); ArrayList<User> data = mSQLite.getAllDATA(); boolean flag = false; for(int i = 0; i < data.size(); i++){ User userdata = data.get(i); if(name.equals(userdata.getName())&&password.equals(userdata.getPassword())){ flag = true; break; }else{ flag = false; } } if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){ if(flag){ Intent intent1 = new Intent(Login.this, MainActivity.class); startActivity(intent1); finish(); Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(Login.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show(); } } else{ Toast.makeText(Login.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show(); } } }); btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent2 = new Intent(Login.this, Register.class); startActivity(intent2); finish(); } }); mSQLite = new DatabaseHelper(Login.this); } }
标签:总结,name,每日,db,3.1,import,android,password,public From: https://www.cnblogs.com/syhxx/p/17169990.html