最近学了一波android,自己尝试做了一波登录界面,分享出来,请求各位大佬不吝斧正
package com.example.androidlogin;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(@Nullable LoginActivity context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, null, version);
}
public MyDatabaseHelper(@Nullable LogoutActivity context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, null, version);
}
public MyDatabaseHelper(@Nullable RegisterActivity context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, null, version);
}
public static final String usertable="create table usertable("
+"_id Varchar(10) primary key,"
+"password Varchar(10))";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(usertable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists usertable");
onCreate(db);
}
}
package com.example.androidlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;
public class LoginActivity<useraccount> extends Activity {
private Button zhuxiao;
private Button denglu;
private Button zhuche;
private EditText useraccount;
private EditText password;
private MyDatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper=new MyDatabaseHelper(this,"user.db",null,1);
final SQLiteDatabase db=dbhelper.getWritableDatabase();
Button zhuxiao = (Button) findViewById(R.id.zhuxiao);
Button denglu = (Button) findViewById(R.id.denglu);
Button zhuche = (Button) findViewById(R.id.zhuche);
final EditText useraccount = (EditText) findViewById(R.id.account);
final EditText password = (EditText) findViewById(R.id.pwd);
denglu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password_check=password.getText().toString();
String usercount = useraccount.getText().toString();
String passwordcheck = null;
if (usercount == null||"".equals(usercount)) {
Toast.makeText(LoginActivity.this,"请输入账号!",Toast.LENGTH_SHORT).show();
}
else {
Cursor cursor=db.rawQuery("select password from usertable where _id=?",new String[]{usercount});
while (cursor.moveToNext()) {
passwordcheck=cursor.getString(0);
}
cursor.close();
if(password_check.equals(passwordcheck)){
Intent choose=new Intent(LoginActivity.this,ChooseActivity.class);
startActivity(choose);
}
else{
Toast.makeText(LoginActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
}
}
}
});
zhuxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent zhu_xiao=new Intent(LoginActivity.this,LogoutActivity.class);
startActivity(zhu_xiao);
}
});
zhuche.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent zhu_che=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(zhu_che);
}
});
}
}
package com.example.androidlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LogoutActivity extends Activity {
private MyDatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logout);
Button querenzhuxiao=(Button)findViewById(R.id.queren_zhuxiao);
final EditText zhuxiaoaccount=(EditText)findViewById(R.id.zhuxiao_account);
dbhelper=new MyDatabaseHelper(this,"user.db",null,1);
final SQLiteDatabase db=dbhelper.getWritableDatabase();
querenzhuxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String zxaccount=zhuxiaoaccount.getText().toString();
String zx_account=null;
if(zxaccount==null||"".equals(zxaccount)){
Toast.makeText(LogoutActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
}
else {
Cursor cursor=db.rawQuery("select _id from usertable where _id=?",new String[]{zxaccount});
while (cursor.moveToNext()) {
zx_account=cursor.getString(0);
}
cursor.close();
if(zxaccount.equals(zx_account)){
db.execSQL("delete from usertable where _id=?",new String[]{zxaccount});
db.close();
Toast.makeText(LogoutActivity.this,"已成功注销账号,请返回登录界面!",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(LogoutActivity.this,"该账号不存在",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
package com.example.androidlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private MyDatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText usercount=(EditText)findViewById(R.id.account);
final EditText password=(EditText)findViewById(R.id.pwd);
Button querenzhuche=(Button)findViewById(R.id.queren_zhuche);
dbhelper=new MyDatabaseHelper(this,"user.db",null,1);
final SQLiteDatabase db=dbhelper.getWritableDatabase();
querenzhuche.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String useraccount=usercount.getText().toString();
String passwod=password.getText().toString();
String useraccount_check=null;
if(useraccount==null||"".equals(useraccount))
Toast.makeText(RegisterActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
else {
Cursor cursor=db.rawQuery("select _id from usertable where _id=?",new String[]{useraccount});
while (cursor.moveToNext()) {
useraccount_check=cursor.getString(0);
}
cursor.close();
if(useraccount.equals(useraccount_check))
Toast.makeText(RegisterActivity.this,"账号已注册,请重新输入",Toast.LENGTH_SHORT).show();
else {
db.execSQL("insert into usertable values(?,?)",new String[]{useraccount,passwod});
db.close();
Toast.makeText(RegisterActivity.this,"已成功注册,请返回登陆界面登录!",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
本需求主要问题是 如何取出后台数据与当前输入数据比较 ,也就是说如何实现 if…else…结构。
我再贴一个布局文件,其他两个布局文件可参考着做
<?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:background="@drawable/login_background"
<!--使界面上方的工程名和背景留白透明,如果这样做要记得修改活动逻辑代码中要继承Activity而不是AppCompatActivity-->
android:theme="@android:style/Theme.Translucent.NoTitleBar"
tools:context=".LoginActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎使用"
android:textSize="50dp"
android:textColor="#6eaae8"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="#000000"
android:text="账号:" />
<EditText
android:layout_width="220dp"
android:layout_height="wrap_content"
android:inputType="none"
android:singleLine="true"
android:id="@+id/account"
android:layout_marginLeft="20dp"
android:hint="请输入账号"
android:gravity="center_horizontal"
android:background="@drawable/round_box"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="密码:"
android:textColor="#000000"/>
<EditText
android:id="@+id/pwd"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:hint="请输入密码"
android:gravity="center_horizontal"
android:background="@drawable/round_box"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_marginRight="50dp"
android:id="@+id/zhuxiao"
android:gravity="center"
android:hint="去注销"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/denglu"
android:background="@drawable/button"
android:text="登录"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="去注册"
android:id="@+id/zhuche"
android:background="@drawable/button"
android:layout_marginLeft="50dp"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>