应用篇1.3 后台登陆界面审计
一、后台登陆界面图:
各种准备工作完毕后,对于这款日程管理类的软件,首先设计一个后台登陆功能。可以通过密码保护自己的日程隐私。
1、初次登陆界面如图1.1所示。
2、如果已经设置了密码,登陆界面如图1.2所示。
二、登陆界面代码
初次登陆:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PasswordRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/default_bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0px"
android:layout_marginRight="15.0px"
android:layout_marginTop="62.0px"
android:paddingBottom="10.0px"
android:paddingTop="21.0px" >
<EditText
android:id="@+id/pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/password_edit_login"
android:hint="@string/input_password"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="40.0sp"
android:saveEnabled="true" />
<EditText
android:id="@+id/pwd_repeat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pwd"
android:background="@drawable/password_edit_login"
android:hint="@string/repeat_password"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="40.0sp"
android:saveEnabled="true" />
<Button
android:id="@+id/ok"
android:layout_width="130.0px"
android:layout_height="42.0px"
android:layout_alignParentRight="true"
android:layout_below="@+id/pwd_repeat"
android:layout_marginRight="12.0dip"
android:layout_marginTop="7.0px"
android:text="@string/ok" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
设置完密码后登陆:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/default_bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0px"
android:layout_marginRight="15.0px"
android:layout_marginTop="62.0px"
android:paddingBottom="10.0px"
android:paddingTop="21.0px" >
<EditText
android:id="@+id/login_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/password_edit_login"
android:hint="@string/input_password"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="40.0sp"
android:saveEnabled="true" />
<Button
android:id="@+id/login"
android:layout_width="130.0px"
android:layout_height="42.0px"
android:layout_alignParentRight="true"
android:layout_below="@+id/login_pwd"
android:layout_marginRight="12.0dip"
android:layout_marginTop="7.0px"
android:text="@string/login" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
业务逻辑代码:
/*
* Copyright (c) 2013 Solidwang. All Rights Reserved
*/
package com.solidwang.something.activity;
import com.solidwang.something.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.solidwang.something.db.DBUtil;
/**
* <p>
* Description: 登陆界面
* </p>
* @author solidwang
* @version 1.0
* @Date 2013-4-24
*/
public class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBUtil db = new DBUtil(this);
String password = db.getPwd();
//第一次登陆需要设置密码
if("".equals(password)) {
Intent intent = new Intent(Login.this, PasswordManager.class);
startActivity(intent);
finish();
return;
}
//如果已经设置密码,进入登陆界面
setContentView(R.layout.login);
}
}
/*
* Copyright (c) 2013 Sohu. All Rights Reserved
*/
package com.solidwang.something.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.solidwang.something.R;
import com.solidwang.something.db.DBUtil;
/**
* <p>
* Description: 密码管理界面
* </p>
* @author solidwang
* @version 1.0
* @Date 2013-4-24
*/
public class PasswordManager extends Activity {
//密码输入框
private EditText pwd, pwd_repeat;
private Button ok;
private DBUtil db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DBUtil(this);
setContentView(R.layout.password_manager);
pwd = (EditText)findViewById(R.id.pwd);
pwd_repeat = (EditText)findViewById(R.id.pwd_repeat);
ok = (Button)findViewById(R.id.ok);
ok.setOnClickListener(new LoginListener());
}
class LoginListener implements OnClickListener {
@Override
public void onClick(View v) {
String password1 = pwd.getText().toString();
String password2 = pwd_repeat.getText().toString();
if("".equals(password1) || "".equals(password2) || password1==null || password2==null) {
Toast.makeText(PasswordManager.this, R.string.password_is_null, Toast.LENGTH_SHORT).show();
} else {
if(password1.equals(password2)) {
if("".equals(db.getPwd())) {
db.insertPwd(password1);
} else {
db.updatePwd(password1);
}
} else {
Toast.makeText(PasswordManager.this, R.string.password_is_different, Toast.LENGTH_SHORT).show();
}
}
}
}
}
数据库操作源码:
/*
* Copyright (c) 2013 Solidwang. All Rights Reserved
*/
package com.solidwang.something.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* <p>
* Description: 数据库操作工具类
* </p>
* @author solidwang
* @version 1.0
* @Date 2013-4-23
*/
public class DBUtil extends SQLiteOpenHelper{
//数据库
private static final String DATABASE_NAME = "something_db";
private static final Integer DATABASE_VERSION = 1;
//登陆表名及字段
private static final String TABLE_LOGIN = "login";
private static final String LOGIN_USER = "admin";
private static final String LOGIN_PWD = "password";
//日程表名及字段
private static final String TABLE_SCHEDULE = "schedule";
private static final String SCHEDULE_ID = "id";
private static final String SCHEDULE_TITLE = "title";
private static final String SCHEDULE_CONTENT = "content";
private static final String SCHEDULE_CREATETIME = "createtime";
//闹钟表名及字段
private static final String TABLE_CLOCK = "clock";
private static final String CLOCK_ID = "id";
private static final String CLOCK_ISOPEN = "isopen";
private static final String CLOCK_DATE = "date";
private static final String CLOCK_TIME = "time";
private static final String CLOCK_RINGS = "rings";
private static final String CLOCK_URI = "uri";
public DBUtil(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//新建login表
String sql = "create table "+TABLE_LOGIN+" ("
+LOGIN_USER+" text, "
+LOGIN_PWD+" text)";
db.execSQL(sql);
//新建schedule表
sql = "create table "+TABLE_SCHEDULE+" ("
+SCHEDULE_ID+" integer primary key autoincrement, "
+SCHEDULE_TITLE+" text, "
+SCHEDULE_CONTENT+" text, "
+SCHEDULE_CREATETIME+" text )";
db.execSQL(sql);
//新建clock表
sql = "create table "+TABLE_CLOCK+" ("
+CLOCK_ID+" integer primary key, "
+CLOCK_ISOPEN+" text, "
+CLOCK_DATE+" text, "
+CLOCK_TIME+" text, "
+CLOCK_RINGS+" text, "
+CLOCK_URI+" text )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists "+TABLE_LOGIN;
db.execSQL(sql);
sql = "drop table if exists "+TABLE_SCHEDULE;
db.execSQL(sql);
sql = "drop table if exists "+TABLE_CLOCK;
db.execSQL(sql);
}
/*
* 返回密码
*/
public String getPwd() {
SQLiteDatabase sld = this.getReadableDatabase();
String where = LOGIN_USER+"=?";
String[] whereValues = {LOGIN_USER};
Cursor cursor = sld.query(TABLE_LOGIN, null, where, whereValues, null, null, null);
if(cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndex(LOGIN_PWD));
} else {
return "";
}
}
/*
* 保存密码
*/
public long insertPwd(String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(LOGIN_USER, LOGIN_USER);
cv.put(LOGIN_PWD, password);
return db.insert(TABLE_LOGIN, null, cv);
}
/*
* 修改密码
*/
public int updatePwd(String password){
SQLiteDatabase db = this.getWritableDatabase();
String where = LOGIN_USER+"=?";
String[] whereValues = {LOGIN_USER};
ContentValues cv = new ContentValues();
cv.put(LOGIN_PWD, password);
return db.update(TABLE_LOGIN, cv, where, whereValues);
}
}