代码简介
功能简介
在这个项目中,主要运用了对于SQLite数据库的(增、删、改、查)基本操作,包含一个闪屏页面,一个欢迎界面,
一个用户登录页面、一个用户注册页面、一个主界面
下载地址
apk文件下载地址:链接:https://share.weiyun.com/5p7tGNJ
全包下载地址:链接:https://share.weiyun.com/5eUc1pA
恭候指点
各页面主要运用
闪屏页面
启动一个子线程进行倒计时处理,即每隔1s发送一个message至handler里面,handler收到信息之后对控件的文本进行
修改,以此做到闪屏页面的倒计时效果,同时,当按钮在子线程结束跳转页面前被点击时,就会自动结束子线程并且直
接开始跳转页面到主页面。
layout代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PassActivity"
android:background="@mipmap/passbgp"
>
<Button
android:id="@+id/btn1"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:background="#cccccccc"
android:text="跳过3"
android:textSize="18sp"
/>
</RelativeLayout>
Activity代码
package com.example.beansten.booklibrary;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class PassActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1;
Boolean wait;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
init();
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
btn1.setText("跳过"+msg.arg1);
}
};
//倒计时线程
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 3;i>=0;i--){
if(wait){
Message mg1 = new Message();
mg1.arg1 = i;
mg1.what = 1;
handler.sendMessage(mg1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(btn1.getText().equals("跳过0")){
Intent intent = new Intent(PassActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
}else{
System.out.println("子线程已被终结");
break;
}
}
}
}).start();
}
private void init() {
wait = true;
btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn1){
wait = false;
Intent intent = new Intent(PassActivity.this,MainActivity.class);
startActivity(intent);
}
}
}
主页面
该页面没有多余业务逻辑,仅仅是一个Button进行页面的切换
登录页面
登录页面提供用户进行用户名和密码输入,在点击登录按钮的同时,首先对数据库进行一次检索,当数据库中存在该用户且密码信息相符时,会跳转到主界面,而当用户名不存在或者密码错误时,会清空两个输入框,并且弹出一个吐丝,同时该页面还可以跳转到注册页面。
layout代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:background="@mipmap/zhucelogin"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
></LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="登录/注册"
android:textColor="#666"
android:textSize="35sp"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="@+id/loginId"
android:hint="账号"
android:textColorHint="#9333"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#8ccc"
android:textColor="#66f"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
></LinearLayout>
<EditText
android:id="@+id/loginKey"
android:hint="密码"
android:textColorHint="#9333"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#8CCC"
android:textColor="#66F"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
></LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
>
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="登录"
android:textSize="20sp"
android:textColor="#D333"
android:layout_centerHorizontal="true"
android:background="#B999"
/>
<TextView
android:id="@+id/loginZhuCe"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="立即注册"
android:textSize="16sp"
android:gravity="center"
android:textColor="#D999"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
>
</LinearLayout>
</LinearLayout>
activity代码
package com.example.beansten.booklibrary;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
EditText loginId,loginKey;
Button loginBtn;
TextView loginZhuCe;
DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
}
//初始化
private void init() {
loginId = findViewById(R.id.loginId);
loginKey = findViewById(R.id.loginKey);
loginBtn = findViewById(R.id.loginBtn);
loginZhuCe = findViewById(R.id.loginZhuCe);
loginBtn.setOnClickListener(this);
loginZhuCe.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginBtn: //提取账号密码并调用数据库进行比对
//提取输入的用户名及密码
Boolean success = false;
String userLoginName = String.valueOf(loginId.getText());
String userLoginKey = String.valueOf(loginKey.getText());
String userName;
String userKey;
dbOpenHelper = new DBOpenHelper(this, "LIBRARY_DB.db", null, 1);
System.out.println("创立新的helper");
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
System.out.println("获取数据库成功");
Cursor cursor = db.query("USERINFO", null, null, null, null, null, null);
System.out.println("获取结果cursor成功");
if(cursor.getCount()==0){
Intent intent2 = new Intent(LoginActivity.this,ZhuCeActivity.class);
startActivity(intent2);
}else{
cursor.moveToFirst();
System.out.println("查询成功");
do {
userName = cursor.getString(cursor.getColumnIndex("USER_NAME"));
userKey = cursor.getString(cursor.getColumnIndex("USER_KEY"));
System.out.println(userName + " " + userKey);
if (userLoginName.equals(userName) && userLoginKey.equals(userKey)) {
//验证通过,弹窗跳转
success = true;
System.out.println("用户登录成功 :" + userName + " " + userKey);
break;
}
} while (cursor.moveToNext());
if (success) {
System.out.println("登陆成功");
Toast.makeText(LoginActivity.this, "欢迎回来 " + userLoginName, Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this,Main2Activity.class);
startActivity(intent);
finish();
} else {
System.out.println("登录失败 ");
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
loginKey.setText("");
}
}
break;
case R.id.loginZhuCe: //跳转到注册Acitivity
Intent intent = new Intent(LoginActivity.this,ZhuCeActivity.class);
startActivity(intent);
break;
}
}
}
注册页面
注册页面有三个输入框,分别要求用户输入账号以及两次密码,用户点击注册时首先监测三个输入框 是否为空,当任何一个为空时都会直接提示用户输入完整信息,当三个都不为空并且两次密码输入的内容一致时,会先检索数据库中是否存在当前用户名,当不存在是,利用insert()方法,将注册信息存放到数据库中,如果已存在,则会提示该用户已注册
layout代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout