zoukankan      html  css  js  c++  java
  • Android开发-记账本APP

    软件界面:

     工程目录:

     源码:

    MainActivity.java

    package com.example.money;
    
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    
    import com.google.android.material.bottomnavigation.BottomNavigationView;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.navigation.NavController;
    import androidx.navigation.Navigation;
    import androidx.navigation.ui.AppBarConfiguration;
    import androidx.navigation.ui.NavigationUI;
    
    
    public class MainActivity extends AppCompatActivity {
    
        DBHelper dbHelper=null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            BottomNavigationView navView = findViewById(R.id.nav_view);
            AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard).build();
            NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
            NavigationUI.setupWithNavController(navView, navController);
    
            dbHelper=new DBHelper(getApplicationContext());
            //show();
        }
        public void clear(){
            EditText in_time = findViewById(R.id.in_time);
            EditText in_num = findViewById(R.id.in_num);
            EditText in_type = findViewById(R.id.in_type);
            RadioGroup b = findViewById(R.id.radioGroup);
            String bb = ((RadioButton)findViewById(b.getCheckedRadioButtonId())).getText().toString();
            in_time.setText("");
            in_num.setText("");
            in_type.setText("");
            b.clearCheck();
        }
        public void insql(){
            EditText in_time = (EditText)findViewById(R.id.in_time);
            EditText in_num = (EditText)findViewById(R.id.in_num);
            EditText in_type = (EditText)findViewById(R.id.in_type);
            RadioGroup b = findViewById(R.id.radioGroup);
    
            String bb = ((RadioButton)findViewById(b.getCheckedRadioButtonId())).getText().toString();
            String time=in_time.getText().toString();
            String num="";
            if(bb.equals("支出")){
                num = "-" + in_num.getText().toString();
            }
            else {
                num = "+" + in_num.getText().toString();
            }
            String type=in_type.getText().toString();
    
            ContentValues contentValues = new ContentValues();
            contentValues.put("time",time);
            contentValues.put("num",num);
            contentValues.put("type",type);
    
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.insert("moneytab",null,contentValues);
    
        }
        public void submit(View v){
            insql();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            //Log.i("点击按钮","记录");
            builder.setMessage("已记录");
            builder.setPositiveButton("确认", null);
            builder.create().show();
            clear();
        }
    
        public void show(View view){
            TextView lv = findViewById(R.id.lv);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from moneytab",null);
            String str="";
            while(cursor.moveToNext()){
                String time = cursor.getString(cursor.getColumnIndex("time"));
                String num = cursor.getString(cursor.getColumnIndex("num"));
                String type = cursor.getString(cursor.getColumnIndex("type"));
                str += time+"   	"+ num +"   	"+ type + "
    ";
            }
            if(str == ""){
                lv.setText("暂无数据");
            }
            else{
                lv.setText(str);
            }
    
        }
    
    }

    DBHelper.java

    package com.example.money;
    
    import android.content.Context;
    import android.database.sqlite.*;
    
    public class DBHelper extends SQLiteOpenHelper {
    
        public DBHelper(Context context){
            super(context,"money.db",null,3);
        }
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table moneytab(time text,num text,type text)");
        }
        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase,int oldVersion,int newVersion) {
    
        }
        @Override
        public void onOpen(SQLiteDatabase sqLiteDatabase){
            super.onOpen(sqLiteDatabase);
        }
    
    }

    dashboard

    package com.example.money.ui.dashboard;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import androidx.annotation.Nullable;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.lifecycle.Observer;
    import androidx.lifecycle.ViewModelProviders;
    
    import com.example.money.R;
    
    public class DashboardFragment extends Fragment {
    
        private DashboardViewModel dashboardViewModel;
    
        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
            dashboardViewModel =
                    ViewModelProviders.of(this).get(DashboardViewModel.class);
            View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
    
            return root;
        }
    
    
    
    }
    package com.example.money.ui.dashboard;
    
    import androidx.lifecycle.LiveData;
    import androidx.lifecycle.MutableLiveData;
    import androidx.lifecycle.ViewModel;
    
    public class DashboardViewModel extends ViewModel {
    
        private MutableLiveData<String> mText;
    
        public DashboardViewModel() {
            mText = new MutableLiveData<>();
            //mText.setValue("收入界面");
        }
    
        public LiveData<String> getText() {
            return mText;
        }
    }

    home

    package com.example.money.ui.home;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import androidx.annotation.Nullable;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.lifecycle.Observer;
    import androidx.lifecycle.ViewModelProviders;
    
    import com.example.money.R;
    
    public class HomeFragment extends Fragment {
    
        private HomeViewModel homeViewModel;
    
        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
            homeViewModel =
                    ViewModelProviders.of(this).get(HomeViewModel.class);
            View root = inflater.inflate(R.layout.fragment_home, container, false);
    
            return root;
        }
    }
    package com.example.money.ui.home;
    
    import androidx.lifecycle.LiveData;
    import androidx.lifecycle.MutableLiveData;
    import androidx.lifecycle.ViewModel;
    
    public class HomeViewModel extends ViewModel {
    
        private MutableLiveData<String> mText;
    
        public HomeViewModel() {
            mText = new MutableLiveData<>();
            //mText.setValue("账本界面");
        }
    
        public LiveData<String> getText() {
            return mText;
        }
    }

    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />
    
        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/mobile_navigation" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <EditText
            android:id="@+id/in_time"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="75dp"
            android:hint="时间"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.502"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
        <EditText
            android:id="@+id/in_num"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="24dp"
            android:hint="金额"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
    
        <EditText
            android:id="@+id/in_type"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="32dp"
            android:hint="描述"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.502"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/in_num" />
    
        <Button
            android:id="@+id/in_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="记录"
            android:onClick="submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/in_type" />
    
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="200dp"
            android:layout_height="65dp"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/in_time">
    
            <RadioButton
                android:id="@+id/in"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="收入" />
    
            <RadioButton
                android:id="@+id/out"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="支出" />
        </RadioGroup>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <TextView
            android:id="@+id/lv"
            android:layout_width="360dp"
            android:layout_height="0dp"
            android:layout_marginBottom="35dp"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="show"
            android:text="浏览账本"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    底侧栏

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/navigation_home"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/title_home" />
    
        <item
            android:id="@+id/navigation_dashboard"
            android:icon="@android:drawable/ic_menu_add"
            android:title="@string/title_dashboard" />
    
    
    </menu>

    valuesstrings.xml

    <resources>
        <string name="app_name">记账本</string>
        <string name="title_home">浏览</string>
        <string name="title_dashboard">记账</string>
    </resources>

    遇到的问题:

    1.账本界面的信息无法直接展示,必须点击按钮再展示。

    2.从数据库读取的信息展示问题,目前是用String字符串记录读取的数据,再用TextView.setText()将TextView框的文字设为String。

    3.目前未实现查询、删除操作。

  • 相关阅读:
    大数据之MapReduce工作机制
    swoft +nginx 快速获取客户端的真实的IP
    JWT 介绍 详解
    swoft 切面编程 (AOP编程思想)
    CSP 编程模型
    Ubuntu 16.04 Mysql 重置密码
    ubuntu 提示:rm: cannot remove 'you-get/tmp': Directory not empty
    Ubuntu 系统升级到php7.2/7.3 (平滑升级)-朝花夕拾
    git clone 非标准的ssh端口(非22)端口
    Ubuntu 升级npm 以及安装cross-env 过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/a8047/p/14471782.html
Copyright © 2011-2022 走看看