您的位置:首页 > 移动开发

℃江让您从精通到入门:App登陆模块的实现

2017-01-22 08:45 302 查看
模块实现前,先说两点:

第一点、登陆模块的实现,是多数APP的基础功能。

第二点、数据库用的不是MySQL,Oracle等,Android项目,多数还是会用到SQLite.

现在开始:

第一步、新建一个Activity(笔者为了阐明清楚,新建了一个module)





点击Finish.

第二步、先编写layout中的activity_login.xml文件(登陆界面形色各异,笔者不做修饰,只是最简单的TextView,EditText,Button):



activity_login.xml源代码如下:

<?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:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.login.LoginActivity"
android:orientation="vertical">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textSize="30dp"
android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/activity_login_et_username"
android:layout_width="278dp"
android:layout_height="wrap_content" />

</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textSize="30dp"
android:text="密    码:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:inputType="textPassword"
android:id="@+id/activity_login_et_pwd"
android:layout_width="293dp"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:text="登陆"
android:id="@+id/activity_login_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>


第三步、新建两个package,分别是sqlite和dao,如下图:







第四步、在sqlite包中(类似于工具类),新建一个Java Class,叫:SQLiteHelper.java。代码如下:

package com.dujiang.MyWealth.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Dujiang0311 on 2017/1/21.
*/

public class SQLiteHelper extends SQLiteOpenHelper{

/*SQLiteHelper的四个参数,上下文,数据库名字,null,版本号(任意数字)*/
public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

/*上面那个太过复杂,所以需要重载一个简单的方法:通过构造方法,完成数据库的创建*/
public SQLiteHelper(Context context){
super(context,"mydb",null,1);
}

/*通过OnCreate方法,实现数据表的创建*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table login (username varchar2(20) , pwd varchar2(20))");
db.execSQL("insert into login values('admin','admin')");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}


第五步、dao层里面新建一个LoginDao.java文件:

package com.example.login.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dujiang.MyWealth.sqlite.SQLiteHelper;

/**
* Created by Dujiang0311 on 2017/1/21.
*/

public class LoginDao {
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase db ;
public LoginDao(Context context){
sqLiteHelper = new SQLiteHelper(context);
}

/*登陆功能*/
public boolean login(String username , String pwd){

//        当数据库中的数据进行查询操作的时候,需要调用getReadableDatabase()
db = sqLiteHelper.getReadableDatabase();
Cursor cursor =  db.query("login", new String[]{"username"},"username = ? and pwd = ?",new String[]{username,pwd},
null,null,null);
if (cursor.moveToNext()){
cursor.close();
db.close();
return true;
}else {
return false;
}
}
}


第六步、在LoginActivity中调用SQLite以及使用相关控件:

package com.example.login;

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.login.MainActivity;
import com.example.login.R;
import com.example.login.dao.LoginDao;

public class LoginActivity extends AppCompatActivity {

//定义控件
private EditText username , pwd ;
private Button loginbtn;
private LoginDao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
dao = new LoginDao(this) ;

//获取控件
username = (EditText) findViewById(R.id.activity_login_et_username);
pwd = (EditText) findViewById(R.id.activity_login_et_pwd);
loginbtn = (Button) findViewById(R.id.activity_login_btn);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//判断登陆成功与否
if(dao.login(username.getText().toString(),pwd.getText().toString())){
//登陆成功页面跳转
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}else {
//登陆失败,显示提示信息
Toast.makeText(LoginActivity.this, "用户名和密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}


第七步、为了在打开APP时第一个就是登陆界面,所以需要配置AndroidManifest.xml文件:



<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>


剪切,放到.LoginActivity中:

<activity android:name=".LoginActivity">
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>


以上就完成了基本的登陆功能实现,数据库中有的用户名可以登陆成功,并且跳转到主界面,没有的,则不能登陆。演示如下:

输入错误的用户名或者密码:



输入正确的用户名密码(admin,admin):

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: