每日总结2023/3/3
今天学习了安卓连接sqlite并且进行登录注册操作
main.xml文件
<?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" tools:context=".MainActivity" android:orientation="vertical" > <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:text="账号:" android:layout_weight="1" android:textSize="30dp" android:gravity="center_vertical"/> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/edname"/> </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:text="密码:" android:layout_weight="1" android:textSize="30dp" android:gravity="center_vertical"/> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/edpassword"/> </LinearLayout> <Button android:layout_width="160dp" android:layout_height="80dp" android:text="登录" android:textSize="27dp" android:id="@+id/login" android:layout_gravity="center"/> <Button android:layout_width="160dp" android:layout_height="80dp" android:text="注册" android:textSize="27dp" android:id="@+id/register" android:layout_gravity="center"/>
register.xml
<?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" tools:context=".register" android:orientation="vertical" > <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:text="账号:" android:layout_weight="1" android:textSize="30dp" android:gravity="center_vertical"/> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/edname1"/> </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:text="密码:" android:layout_weight="1" android:textSize="30dp" android:gravity="center_vertical"/> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/edpassword1"/> </LinearLayout> <Button android:layout_width="160dp" android:layout_height="80dp" android:text="注册" android:textSize="27dp" android:id="@+id/register1" android:layout_gravity="center" android:onClick="zhuche" /> </LinearLayout>
第二跳转页面good.xml,这个可以随便写
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="com.example.a86191.mylogin.good"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="登陆成功" android:textSize="100dp"/> </android.support.constraint.ConstraintLayout>
user的java
package com.example.a86191.mylogin.javabean; /** * Created by 86191 on 2023/3/2. */ public class User { private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return password */ public String getPassword() { return password; } /** * 设置 * @param password */ public void setPassword(String password) { this.password = password; } public String toString() { return "User{name = " + name + ", password = " + password + "}"; } }
good
package com.example.a86191.mylogin; import android.support.v7.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); } }
MainActivity
package com.example.a86191.mylogin; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.a86191.mylogin.javabean.User; 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.edname); password=findViewById(R.id.edpassword); login.setOnClickListener(this); register.setOnClickListener(this); } @Override public void onClick(View view){ int id=view.getId(); switch (id){ case R.id.login: String s=name.getText().toString(); String s1=password.getText().toString(); Boolean login= mYsqLiteopenhelper.login(s,s1); if (login){ Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show(); 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,com.example.a86191.mylogin.register.class); startActivity(i1); break; } } }
MYsqLiteopenhelper
package com.example.a86191.mylogin; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.support.annotation.Nullable; import com.example.a86191.mylogin.javabean.User; import java.util.ResourceBundle; /** * Created by 86191 on 2023/3/2. */ public class MYsqLiteopenhelper 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 MYsqLiteopenhelper(@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 false; }}
register
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.a86191.mylogin.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(); } public void find(){ register1=findViewById(R.id.register1); name1=findViewById(R.id.edname1); password1=findViewById(R.id.edpassword1); } public void zhuche(View view) { String s=name1.getText().toString(); String s1=password1.getText().toString(); User u =new User(s,s1); long l=mYsqLiteopenhelper1.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(); } } }