初學 Android項目 {家庭理財通}
我也是一個初學Android的小白,在校大二項目希望對大家有幫助!
項目文件 項目文件中 有 密碼的Model、Dao、以及創建新密碼的Activity 只不過當時沒有實現。小夥伴們可以在系統設置中自己練習來完善功能
所以登錄時設定了初始密碼
密碼爲 admin 小寫
eclipse | JDK |
---|---|
Android 4.3 | JDK1.8 |
使用 Genymotion 2.12 虛擬器 來模擬 Android 手機運行 源碼下載
文件樹
AccountTools 爲彈窗工具類 使用時直接調用即可
AccountTools.java
代碼片
.
package com.ds.accountsoft.tools;
import com.ds.accountsoft.model.InAccount;
import com.ds.accountsoft.model.OutAccount;
import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;
public class AccountTools {
private Context context;
public static void showToast(Context context,String text){
Toast t = Toast.makeText(context, text, Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP, 0, 333);
t.show();
}
/**將新Account實例*/
public static OutAccount checkOut(String str_money,String str_time,String str_type,String str_address,String str_mark){
OutAccount out = new OutAccount();
double money = 0 ;
if(str_money != null && !str_money.equals("")){
try{
money = Double.valueOf(str_money).doubleValue();
out.setMoney(money);
}catch(Exception e){
out.setMoney(0);
}
}
if(str_time != null && !str_time.equals("")){
out.setTime(str_time);
}else{
out.setTime("");
}
if(str_type != null && !str_type.equals("")){
out.setType(str_type);
}else{
out.setType("");
}
if(str_address != null && !str_address.equals("")){
out.setAddress(str_address);
}else{
out.setAddress("");
}
if(str_mark != null && !str_mark.equals("")){
out.setMark(str_mark);
}else{
out.setMark("");
}
return out;
}
public static InAccount checkAdd(String str_money,String str_time,String str_type,String str_payfor,String str_mark){
InAccount add = new InAccount();
double money = 0 ;
if(str_money != null && !str_money.equals("")){
try{
money = Double.valueOf(str_money).doubleValue();
add.setMoney(money);
}catch(Exception e){
add.setMoney(0);
}
}
if(str_time != null && !str_time.equals("")){
add.setTime(str_time);
}else{
add.setTime("");
}
if(str_type != null && !str_type.equals("")){
add.setType(str_type);
}else{
add.setType("");
}
if(str_payfor != null && !str_payfor.equals("")){
add.setPayfor(str_payfor);
}else{
add.setPayfor("");
}
if(str_mark != null && !str_mark.equals("")){
add.setMark(str_mark);
}else{
add.setMark("");
}
return add;
}
}
登錄界面樣式
需要先在strings.xml中添加所需要用到的 字段並定義一個name 以便使用 下面這是我定義的所有頁面中將使用的文字
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">家庭理財通</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="txt_pwd">請輸入密碼:</string>
<string name="edit_hint">請輸入密碼....</string>
<string name="btn_login">登錄</string>
<string name="btn_exit">退出</string>
<string name="txt_out_title">新增支出</string>
<string name="txt_out_money">金額:</string>
<string name="txt_out_time">時間:</string>
<string name="txt_out_type">類別:</string>
<string name="txt_out_address">地點:</string>
<string name="txt_out_mark">備註:</string>
<string name="btn_save">保存</string>
<string name="btn_reset">取消</string>
<string name="hint_out_money">金額....</string>
<string name="hint_out_time">選擇時間....</string>
<string name="hint_out_type">選擇類別....</string>
<string name="hint_out_address">地址....</string>
<string name="hint_out_mark">備註....</string>
<string name="txt_add_title">新增收入</string>
<string name="txt_add_money">金    額:</string>
<string name="txt_add_time">時    間:</string>
<string name="txt_add_type">類    別:</string>
<string name="txt_add_payfor">付款方:</string>
<string name="txt_add_mark">備    注:</string>
<string name="hint_add_money">金額....</string>
<string name="hint_add_time">選擇時間....</string>
<string name="hint_add_type">類別....</string>
<string name="hint_add_payfor">付款方....</string>
<string name="hint_add_mark">備註....</string>
<string name="txt_myout_title">我的支出</string>
<string name="txt_myadd_title">我的收入</string>
<!-- 數據管理 -->
<string name="txt_manager_title">數據管理</string>
<string name="btn_manager_in">我的收入</string>
<string name="btn_manager_out">我的支出</string>
<string name="txt_edit_out_title">編輯支出</string>
<string name="txt_edit_add_title">編輯收入</string>
<string name="btn_delete">刪除</string>
<string name="btn_replace">修改</string>
<!-- 系統設置 -->
<string name="txt_password_title">系統設置</string>
<string name="btn_password_add">新增密碼</string>
<string name="btn_password_update">修改密碼</string>
<string name="txt_add_password_title">增加密碼</string>
<string name="txt_edit_password_title">修改密碼</string>
<string name="txt_input_password">輸入密碼:</string>
<string name="hint_input_password">請輸入密碼....</string>
<string name="txt_again_input_password">確認輸入:</string>
<string name="hint_again_input_password">請再次輸入密碼....</string>
<string name="txt_flag_title">收支便籤</string>
<string name="btn_flag_add">新增便籤</string>
<string name="btn_flag_update">修改便籤</string>
<string name="txt_add_flag_title">新增便籤</string>
<string name="txt_update_flag_title">編輯便籤</string>
<string name="hint_add_message">請輸入便籤....</string>
</resources>
account_login.xml
代碼片
.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:background="@drawable/bgimg">
<TextView
android:id="@+id/txt_pwd"
android:text="@string/txt_pwd"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/steelblue"
/>
<EditText
android:layout_marginTop="20dp"
android:id="@+id/edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_hint"
android:inputType="textPassword"
android:textSize="16sp"
android:layout_below="@id/txt_pwd"
/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/btn_login"
android:text="@string/btn_login"
android:layout_height="40dp"
android:layout_width="match_parent"
android:layout_below="@id/edit_pwd"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/btn_exit"
android:text="@string/btn_exit"
android:layout_height="40dp"
android:layout_width="match_parent"
android:layout_below="@id/btn_login"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
passwordDao.java
DBHelper.java
DBHelper.java 是在手機中創建數據庫與數據表
弊端 軟件如果卸載 在重新安裝數據將會消失不會保存
package com.ds.accountsoft.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
private static String DB_NAME = "db_account";
private static int VERSION = 1;
private static String TB_IN ="create table tb_in(_id Integer primary key autoincrement,money,time,type,payfor,mark)";
private static String TB_OUT ="create table tb_out(_id Integer primary key autoincrement,money,time,type,address,mark)";
private static String TB_FLAG ="create table tb_flag(_id Integer primary key autoincrement,message)";
private static String TB_PWD ="create table tb_pwd(_id Integer primary key autoincrement,pwd)";
public DBHelper(Context context){
super(context,DB_NAME,null,VERSION);
}
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TB_IN);
db.execSQL(TB_OUT);
db.execSQL(TB_FLAG);
db.execSQL(TB_PWD);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int oldversion, int newversion) {
}
}
Accout_LoginActivity.java
package com.ds.accountsoft.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.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.ds.accountsoft.dao.PasswordDao;
import com.ds.accountsoft.model.Password;
import com.ds.accountsoft.tools.*;
public class Accout_LoginActivity extends Activity {
private Context context = Accout_LoginActivity.this;
private Button btn_login,btn_exit;
private TextView txt_pwd;
private EditText edit_pwd;
private void bindView(){
btn_login = (Button)findViewById(R.id.btn_login);
btn_exit = (Button)findViewById(R.id.btn_exit);
txt_pwd = (TextView)findViewById(R.id.txt_pwd);
edit_pwd = (EditText)findViewById(R.id.edit_pwd);
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String pwd = edit_pwd.getText()+"";
if(pwd == null || pwd.equals("")){
AccountTools.showToast(context, "請輸入密碼!");
}else{
if(pwd.equals("admin")){ //設置登錄密碼
Intent intent = new Intent(context,AccountMainActivity.class);
startActivity(intent);
AccountTools.showToast(context, "進入主程序");
}else{
AccountTools.showToast(context, "密碼輸入錯誤請從新輸入");
edit_pwd.setText("");
}
}
}
});
btn_exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();//退出此程序
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_login);
bindView();
}
}
AndroidMainifest.xml
AndroidMainifest.xml 實現開始主頁與各頁面跳轉
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ds.accountsoft.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ico"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ds.accountsoft.activity.Accout_LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AccountMainActivity"/><!-- 應用主界面 -->
<activity android:name=".AccountAddOutActivity"/><!-- 新增支出 -->
<activity android:name=".AccountAddInActivity"/><!-- 新增收入-->
<activity android:name=".AccountMyOutActivity"/><!-- 我的支出 -->
<activity android:name=".AccountMyAddActivity"/><!-- 我的收入 -->
<activity android:name=".AccountManagerActivity"/><!-- 數據管理 -->
<activity android:name=".AccountEditOutActivity"/><!-- 修改新增支出 -->
<activity android:name=".AccountEditInActivity"/><!-- 修改新增收入-->
<activity android:name=".AccountSystemActivity"/><!-- 系統管理 -->
<activity android:name=".AccountAddPassword"/><!-- 增加密碼 -->
<activity android:name=".AccountFlagActivity"/><!-- 收支便籤 -->
<activity android:name=".AccountAddMessageActivity"/><!-- 添加便籤 -->
</application>
</manifest>
AccountMainActivity.java 主頁顯示
package com.ds.accountsoft.activity;
import com.ds.accountsoft.tools.AccountTools;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
public class AccountMainActivity extends Activity {
private Context context = AccountMainActivity.this;
private GridView gridview;
private int [] images =
{R.drawable.increase,R.drawable.decrease,
R.drawable.easyicon,R.drawable.cance,
R.drawable.data,R.drawable.settings,
R.drawable.setting,R.drawable.power};
private String [] names=
{"新增支出","新增收入","我的支出","我的收入",
"數據管理","系統設置","收支便籤","系統退出"};
private void bindView(){
gridview = (GridView)findViewById(R.id.Grd_arent);
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(context);
TextView txt = new TextView(context);
txt.setText(names[position]);
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
txt.setLayoutParams(params);
txt.setGravity(Gravity.CENTER);
Drawable top = getResources().getDrawable(images[position]);
top.setBounds(0, 0, 90,90);
txt.setCompoundDrawables(null, top, null, null);
layout.addView(txt);
return layout;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public int getCount() {
return names.length;
}
};
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int which,
long arg3) {
String text = "進入"+names[which]+"子界面...";
switch(which){
case 0:
Intent out_intent = new Intent(context,AccountAddOutActivity.class);/**新增支出*/
startActivity(out_intent);
AccountTools.showToast(context, text);break;
case 1:
Intent in_intent = new Intent(context,AccountAddInActivity.class);/**新增收入*/
startActivity(in_intent);
AccountTools.showToast(context, text);break;
case 2:
Intent myout_intent = new Intent(context,AccountMyOutActivity.class);/**我的支出*/
startActivity(myout_intent);
AccountTools.showToast(context, text);break;
case 3:
Intent myin_intent = new Intent(context,AccountMyAddActivity.class);/**我的收入*/
startActivity(myin_intent);
AccountTools.showToast(context, text);break;
case 4:
Intent manager_intent = new Intent(context,AccountManagerActivity.class);/**數據管理*/
startActivity(manager_intent);
AccountTools.showToast(context, text);break;
case 5:
Intent password_intent = new Intent(context,AccountSystemActivity.class);/**系統設置*/
startActivity(password_intent);
AccountTools.showToast(context, text);break;
case 6:
Intent flag_intent = new Intent(context,AccountFlagActivity.class);/**收支便籤*/
startActivity(flag_intent);
AccountTools.showToast(context, text);break;
case 7: finish();break;
}
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_main);
bindView();
}
}
AccountAddOutActivity.java 新增支出
AccountMyAddActivity.java 我的收入
AccountManagerActivity.java 數據管理
AccountFlagActivity.java 收支便籤
還有的頁面也就不放圖了因爲都不盡相同
比如新增支出 與 新增收入 、我的支出與我的收入、 還有的
是在數據管理中點擊簡單提示進入 ,
AccountEditOutActivity(修改新增支出)是回顯到像新增支出一樣的頁面來添加修改與刪除事件
AccountEditInActivity(修改新增收入)與它相同
AccountAddPassword 增加密碼事件 未與登錄文本框綁定判斷
系統退出
可執行APK
切記每次修改源碼後執行將需要再次發送安裝來更新
Android_家庭理財通
【CSDN】
https://download.csdn.net/download/qq_43466804/11615525