一、歡迎界面的設計與實作
1、界面效果圖

2、前期準備作業
(1)背景圖片

(2)樣式檔案
(3)樣式檔案 1:btn_style_one.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#ffffff" android:width="1dp"/>
<solid android:color="#575d91"/>
<corners android:radius="20dp"/>
</shape>
(4)樣式檔案 2:btn_style_two.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#6A6969" android:width="1dp"/>
<solid android:color="#eeeeee"/>
<corners android:radius="20dp"/>
</shape>
(5)樣式檔案 3:btn_style_three.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#ffffff" android:width="1dp"/>
<corners android:radius="20dp"/>
</shape>
(6)選擇工程的包名,新建另外的三個頁面
類檔案名稱如下:
歡迎界面:WelcomeActivity
注冊界面:RegisterActivity
登錄界面:LoginActivity

3、布局界面 activity_welcome.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"
android:orientation="vertical"
android:background="@drawable/welcomebg"
tools:context=".WelcomeActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:src="@drawable/wel_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="錢都花哪了?"
android:textSize="30sp"
android:textColor="#ffffff"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="記錄生活,點亮財富人生!"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/bt_know_wel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="知道了"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="@drawable/btn_style_three"/>
</LinearLayout>
4、類檔案 WelcomeActivity.java
public class WelcomeActivity extends AppCompatActivity {
//定義物件
Button bt_know_wel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//系結控制元件
bt_know_wel=findViewById(R.id.bt_know_wel);
//按鈕單擊事件
bt_know_wel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
二、注冊界面的設計與實作
1、界面效果圖

2、前期準備作業
(1)設定程式從注冊界面啟動
打開工程的組態檔 AndroidManifest.xml,將程式啟動的四行代碼放置到注冊界面的開始節 點與結束節點之間,如下圖所示,
3、布局界面 activity_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"
android:orientation="vertical"
android:background="@drawable/registerbg"
tools:context=".view.RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="260dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_name_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_pwd_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="郵 箱"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_email_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電 話"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_phone_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<Button
android:id="@+id/bt_ok_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注冊"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/btn_style_one"
android:layout_marginTop="40dp"/>
<Button
android:id="@+id/bt_cancel_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/btn_style_two"
android:layout_marginTop="20dp"/>
</LinearLayout>
4、創建資料庫類檔案 MyDBHelper.java
(1)建立一個 db 檔案夾
(1)右鍵單擊工程的包名——新建——Package——在目前已有包名的后面資料 db——OK, 便可在目前工程的包名下方出現了 db 檔案夾,里面放置資料庫類檔案,
(2)在 db 檔案夾中建立資料庫類檔案
選擇 db——右擊——新建——javaclass——輸入類名:MyDBHelper——super class 中輸入父類:SQLiteOpenHelper——單擊 OK 按鈕,
(3)資料庫類檔案代碼
public class MyDBHelper extends SQLiteOpenHelper {
private static final String DBNAME="financial.db";
private static final int VERSION=1;
public MyDBHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
//1 創建資料庫
@Override
public void onCreate(SQLiteDatabase db) {
//創建用戶表
db.execSQL("create table tb_userinfo(id integer primary key autoincrement,name varchar(10),pwd varchar(15),email varchar(50),phone varchar(11))");
}
//2 升級資料庫
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
5、注冊界面類檔案 RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
//3 定義物件
EditText et_name,et_pwd,et_email,et_phone;
Button btn_register,btn_cancel;
MyDBHelper mhelper;//創建一個資料庫類檔案
SQLiteDatabase db;//創建一個可以操作的資料庫物件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//4 系結控制元件
initView();
//5 注冊按鈕功能的實作
btnRegister();
//6 取消按鈕功能的實作
btnCancel();
}
//4 系結控制元件--------代碼
private void initView() {
et_name=findViewById(R.id.et_name_rg);
et_pwd=findViewById(R.id.et_pwd_rg);
et_email=findViewById(R.id.et_email_rg);
et_phone=findViewById(R.id.et_phone_rg);
btn_register=findViewById(R.id.bt_ok_rg);
btn_cancel=findViewById(R.id.bt_cancel_rg);
mhelper=new MyDBHelper(RegisterActivity.this);
db=mhelper.getWritableDatabase();
}
//5 注冊按鈕功能的實作--------------------代碼
private void btnRegister() {
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//創建一個物件,用來封裝一行資料
ContentValues values=new ContentValues();
values.put("name",et_name.getText().toString());//將輸入的用戶名放到 name 列
values.put("pwd",et_pwd.getText().toString());//將輸入的密碼放到 pwd 列
values.put("email",et_email.getText().toString());//將輸入的郵箱放到 email 列
values.put("phone",et_phone.getText().toString());//將輸入的電話放到 phone 列
//將封裝好的一行資料保存到資料庫的 tb_userinfo 表中
db.insert("tb_userinfo",null,values);
Toast.makeText(RegisterActivity.this,"注冊成功",Toast.LENGTH_SHORT).show();
}
});
}
//6 取消按鈕功能的實作-------------------代碼
private void btnCancel() {
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
三、登陸界面
1、界面效果

2、登錄界面布局 activity_login.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"
android:orientation="vertical"
android:background="@drawable/loginbg"
tools:context=".view.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="260dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_name_lg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼"
android:textSize="15sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_pwd_lg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:inputType="textPassword"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#3700b3"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"/>
<Button
android:id="@+id/bt_newregister_lg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用戶注冊"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@null"
android:layout_marginTop="20dp"
android:layout_gravity="right"/>
<Button
android:id="@+id/bt_login_lg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/btn_style_one"
android:layout_marginTop="40dp"/>
</LinearLayout>
3、登錄界面類檔案 LoginActivity.java
public class LoginActivity extends AppCompatActivity {
//1 定義物件
EditText et_name,et_pwd;
Button btn_newregister,btn_login;
MyDBHelper mhelper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//2 系結控制元件
initView();
//3 登錄按鈕功能的實作
btnLogin();
//4 新用戶注冊按鈕功能的實作
btnNewRegister();
}
//2 系結控制元件--------------------代碼
private void initView() {
et_name=findViewById(R.id.et_name_lg);
et_pwd=findViewById(R.id.et_pwd_lg);
btn_newregister=findViewById(R.id.bt_newregister_lg);
btn_login=findViewById(R.id.bt_login_lg);
mhelper=new MyDBHelper(LoginActivity.this);
db=mhelper.getWritableDatabase();
}
//3 登錄按鈕功能的實作---------------------------代碼
private void btnLogin() {
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先:獲取輸入的用戶名和密碼
String inputname=et_name.getText().toString();
String inputpwd=et_pwd.getText().toString();
//其次:對獲取的用戶名和密碼進行判斷
if(inputname.equals("")||inputpwd.equals("")){//用戶名或密碼為空
Toast.makeText(LoginActivity.this,"用戶名或密碼不能為空",Toast.LENGTH_SHORT).show();
}else{//用戶名或密碼不為空時,我們再對輸入的正確性進行判斷,
// 根據輸入的用戶名和密碼從資料庫中查詢
Cursor cursor =db.rawQuery("select * from tb_userinfo where name=? and pwd=?",new String[]{inputname,inputpwd});
//根據查詢到的結果進行判斷
if (cursor.moveToNext()){//查詢到時
String getname=cursor.getString(cursor.getColumnIndex("name"));
String getpwd=cursor.getString(cursor.getColumnIndex("pwd"));
if(inputname.equalsIgnoreCase(getname)&&inputpwd.equalsIgnoreCase(getpwd)){
Toast.makeText(LoginActivity.this,"用戶名和密碼正確,歡迎登陸",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}else{//沒有查詢到結果時
Toast.makeText(LoginActivity.this,"用戶名或密碼錯誤,請重新輸入",Toast.LENGTH_SHORT).show();
et_name.setText("");
et_pwd.setText("");
}
}
}
});
}
//4 新用戶注冊按鈕功能的實作------------------------代碼
private void btnNewRegister() {
btn_newregister.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/297605.html
標籤:其他

