可在此基础上进行二次开发!
源码地址:源码请点击这里哟!
总的布局:
1、先创建一个空的项目
选好后直接Finish就行
2、对activity_mian,xml文件进行布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="200dp"
android:text="用户名:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="280dp"
android:text="密码:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="190dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<EditText
android:id="@+id/edpassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="270dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="380dp"
android:text="登录"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:layout_marginEnd="80dp"
android:text="注册"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、MainActivity代码
package com.example.mypracticeone;
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 EditText edname,edpassword;
private Button login,register;
private MySqliteHelper mySqliteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySqliteHelper = new MySqliteHelper(this);
find();
}
private void find() {
edname = findViewById(R.id.edname1);
edpassword = findViewById(R.id.edpassword1);
login = findViewById(R.id.login);
register = findViewById(R.id.register);
login.setOnClickListener(this);
register.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
case R.id.login:
String s = edname.getText().toString();
String s1 = edpassword.getText().toString();
boolean login = mySqliteHelper.login(s,s1);
if (login){
Intent i = new Intent(this,Good.class);
startActivity(i);
}else {
Toast.makeText(this,"登陆失败!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.register:
Intent i1 = new Intent(this,Register.class);
startActivity(i1);
break;
}
}
}
4、User类的代码:
package com.example.mypracticeone.bean;
public class User {
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
5、MySqliteHelper类的代码:
package com.example.mypracticeone;
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.mypracticeone.bean.User;
public class MySqliteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "MYsqlite.db";
private static final String create_users = "create table users(name varchar(32),password varchar(32))";
public MySqliteHelper(@Nullable Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(create_users);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long register(User u){
SQLiteDatabase db = getWritableDatabase();
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 = getWritableDatabase();
boolean result = false;
Cursor users = db1.query("users",null,"name like ?",new String[]{name},null,null,null);
if(users != null){
while (users.moveToNext()){
String password1 = users.getString(1);
result = password1.equals(password);
return result;
}
}
return result;
}
}
6、Good与Register类都为创建的Empty Activity
文章来源:https://www.toymoban.com/news/detail-495349.html
7、Good类的代码:
package com.example.mypracticeone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Good extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_good);
}
}
activity_good.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Good">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="200dp"
android:text="HelloWorld"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
8、Register类的代码:
package com.example.mypracticeone;
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.mypracticeone.bean.User;
public class Register extends AppCompatActivity{
private EditText edname1,edpassword1;
private Button register1;
private MySqliteHelper mySqliteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mySqliteHelper = new MySqliteHelper(this);
find();
}
private void find(){
edname1 = findViewById(R.id.edname1);
edpassword1 = findViewById(R.id.edpassword1);
register1 = findViewById(R.id.login);
}
public void register(View view) {
String s = edname1.getText().toString();
String s1 = edpassword1.getText().toString();
User u = new User(s,s1);
long l = mySqliteHelper.register(u);
if(l!=-1){
Toast.makeText(this,"注册成功!",Toast.LENGTH_SHORT).show();
Intent i3 = new Intent(this,MainActivity.class);
startActivity(i3);
}else {
Toast.makeText(this,"注册失败!",Toast.LENGTH_SHORT).show();
}
}
}
activity_register.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Register">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="200dp"
android:text="用户名:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="280dp"
android:text="密码:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="190dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<EditText
android:id="@+id/edpassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="270dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="380dp"
android:text="注册"
android:onClick="register"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
文章来源地址https://www.toymoban.com/news/detail-495349.html
喜欢的话就点赞收藏关注一下吧,要不然到以后就找不到了哟!!!
到了这里,关于AndroidStudio基于Sqlite的登录注册(保姆级教程)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!