zoukankan      html  css  js  c++  java
  • 家庭记账本day6

    记一笔功能实现

    AddActivity文件代码:

    package com.example.keep;
    
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import static com.example.keep.Constants.TABLE_NAME;
    
    public class AddActivity extends AppCompatActivity implements View.OnClickListener  {
        private DatabaseHelper myHelper;
        private RadioButton ra_shouru;
        private RadioButton ra_zhichu;
        private EditText et_money;
        private EditText et_date;
        private EditText et_type;
        private EditText et_way;
    
        private TextView mTvShow;
        private Button mBtAdd;
        private Button mBtQuery;
        private Button mBtUpdate;
        private Button mBtDelete;
    
        public String in="";
        public String out="";
        public String money;
        public String date;
        public String type;
        public String way;
    
        public SQLiteDatabase db;
        public ContentValues values;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add);
            myHelper = new DatabaseHelper(this);
            init();
    
        }
        //实例化界面,获取各个控件的值
        private void init() {
            ra_shouru=(RadioButton) findViewById(R.id.shouru);
            ra_zhichu=(RadioButton) findViewById(R.id.zhichu);
            et_money=(EditText) findViewById(R.id.edit_money);
            et_date=(EditText) findViewById(R.id.edit_date);
            et_type=(EditText) findViewById(R.id.edit_type);
            et_way=(EditText) findViewById(R.id.edit_way);
            mTvShow = (TextView) findViewById(R.id.ld_show);
            mBtAdd = (Button) findViewById(R.id.btn_add);
            mBtUpdate = (Button) findViewById(R.id.btn_update);
            mBtQuery = (Button) findViewById(R.id.btn_query);
            mBtDelete = (Button) findViewById(R.id.btn_delete);
            mBtAdd.setOnClickListener(this);
            mBtUpdate.setOnClickListener(this);
            mBtQuery.setOnClickListener(this);
            mBtDelete.setOnClickListener(this);
        }
        @Override
        //点击事件
        public void onClick(View v) {
            switch(v.getId()) {
                //添加数据
                case R.id.btn_add:
                    addText();
                    queryText();
                    break;
                //查询数据
                case R.id.btn_query:
                    queryText();
                    break;
                //修改数据
                case R.id.btn_update:
                    updateText();
                    queryText();
                    break;
                //删除数据
                case R.id.btn_delete:
                    deleteText();
                    queryText();
                    break;
            }
        }
        //添加数据
        public void addText() {
            money = et_money.getText().toString(); //获取金额
            date = et_date.getText().toString(); //获取时间
            type=et_type.getText().toString();   //获取类别
            way=et_way.getText().toString();     //获取方式
            //获取可读写的SQLiteDatabase对象
            db = myHelper.getWritableDatabase();
            //创建ContentValues对象
            values = new ContentValues();
            //将数据添加到ContentValues对象
            if(ra_shouru.isChecked()){
                in="收入";
                values.put("in_out",in);
            }
            if(ra_zhichu.isChecked()){
                out="支出";
                values.put("in_out",out);//存性别
            }
            values.put("money",money);
            values.put("date",date);
            values.put("type",type);
            values.put("way",way);
    
            if(in.equals("")&&out.equals("")||et_money.getText().toString().equals("")|| et_date.getText().toString().equals("")|| et_way.getText().toString().equals("")||et_type.getText().toString().equals(""))
            {
                Toast.makeText(AddActivity.this, "信息不全,请补充", Toast.LENGTH_SHORT).show();
            }
            else{
                db.insert(TABLE_NAME,null,values);
                //注意别漏掉
                values.clear();
                Toast.makeText(AddActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                et_money.setText("");//保存成功清空信息
                et_date.setText("");
                et_type.setText("");
                et_way.setText("");
            }
            db.close();
        }
        //查询数据
        public void queryText() {
            db=myHelper.getWritableDatabase();
            Cursor cursor = db.query(TABLE_NAME,null,null,null,null,null,null);
            if(cursor.getCount() == 0) {
                mTvShow.setText("");
                Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
            }
            else{
                cursor.moveToFirst();
                mTvShow.setText(cursor.getString(1) + "		" +  cursor.getString(2) + "元		" + cursor.getString(3) +"	 "+cursor.getString(4)+"	 "+ cursor.getString(5));
            }
            while(cursor.moveToNext()) {
                mTvShow.append("
    " + cursor.getString(1) + "		" + cursor.getString(2) + "元		" + cursor.getString(3) +"	 "+cursor.getString(4)+"	 "+ cursor.getString(5));
            }
            cursor.close();
            db.close();
        }
        //修改数据
        public void updateText() {
            db=myHelper.getWritableDatabase();
            //实例化一个要修改的数据的对象
            values=new ContentValues();
            values.put("date",date = et_date.getText().toString());
            values.put("type",type = et_type.getText().toString());
            values.put("way",way = et_way.getText().toString());
            //更新并得到行数
            db.update(TABLE_NAME,values,"money = ?",new String[]{et_money.getText().toString()});
            Toast.makeText(this, "数据修改成功", Toast.LENGTH_SHORT).show();
            db.close();
        }
        //删除数据
        public void deleteText() {
            SQLiteDatabase db=myHelper.getWritableDatabase();
            int i = db.delete(TABLE_NAME,"money=?",new String[]{et_money.getText().toString()});
            if(i !=0 ) {
                Toast.makeText(this,"删除数据成功",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"删除数据失败",Toast.LENGTH_SHORT).show();
            }
            db.close();
        }
    }

    activity_add.xml文件代码:

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     android:orientation="vertical"
      4     android:layout_width="match_parent"
      5     android:layout_height="match_parent"
      6     android:background="@mipmap/bj_2"
      7     >
      8 
      9     <!--标题栏-->
     10     <include layout="@layout/titlebar" />
     11     <RadioGroup
     12         android:id="@+id/RadioGroup1"
     13         android:layout_width="wrap_content"
     14         android:layout_height="wrap_content"
     15         android:orientation="horizontal"
     16         >
     17         <RadioButton
     18             android:id="@+id/shouru"
     19             android:layout_width="wrap_content"
     20             android:layout_height="wrap_content"
     21             android:text="收 入  "
     22             android:textSize="20dp"/>
     23         <RadioButton
     24             android:id="@+id/zhichu"
     25             android:layout_width="wrap_content"
     26             android:layout_height="wrap_content"
     27             android:text="支 出"
     28             android:textSize="20dp"/>
     29     </RadioGroup>
     30 
     31     <LinearLayout
     32         android:layout_width="wrap_content"
     33         android:layout_height="wrap_content"
     34         android:orientation="horizontal">
     35         <TextView
     36             android:layout_width="wrap_content"
     37             android:layout_height="wrap_content"
     38             android:text="金额:"
     39             android:textSize="20dp"
     40             android:height="100px"/>
     41         <EditText
     42             android:id="@+id/edit_money"
     43             android:layout_width="280sp"
     44             android:layout_height="wrap_content"
     45             android:hint="请输入金额"
     46             android:maxLines="1"
     47             android:scrollbars="vertical"
     48             />
     49     </LinearLayout>
     50     <LinearLayout
     51         android:layout_width="wrap_content"
     52         android:layout_height="wrap_content"
     53         android:orientation="horizontal">
     54         <TextView
     55             android:layout_width="wrap_content"
     56             android:layout_height="wrap_content"
     57             android:text="日期:"
     58             android:textSize="20dp"
     59             android:height="100px"/>
     60         <EditText
     61             android:id="@+id/edit_date"
     62             android:layout_width="280sp"
     63             android:layout_height="wrap_content"
     64             android:hint="请输入时间"
     65             android:maxLines="1"
     66             android:scrollbars="vertical"
     67             />
     68     </LinearLayout>
     69     <LinearLayout
     70         android:layout_width="wrap_content"
     71         android:layout_height="wrap_content"
     72         android:orientation="horizontal">
     73         <TextView
     74             android:layout_width="wrap_content"
     75             android:layout_height="wrap_content"
     76             android:text="类别:"
     77             android:textSize="20dp"
     78             android:height="100px"/>
     79         <EditText
     80             android:id="@+id/edit_type"
     81             android:layout_width="280sp"
     82             android:layout_height="wrap_content"
     83             android:hint="请输入类别"
     84             android:maxLines="1"
     85             android:scrollbars="vertical"
     86             />
     87     </LinearLayout>
     88     <LinearLayout
     89         android:layout_width="wrap_content"
     90         android:layout_height="wrap_content"
     91         android:orientation="horizontal">
     92         <TextView
     93             android:layout_width="wrap_content"
     94             android:layout_height="wrap_content"
     95             android:text="方式:"
     96             android:textSize="20dp"
     97             android:height="100px"/>
     98         <EditText
     99             android:id="@+id/edit_way"
    100             android:layout_width="280sp"
    101             android:layout_height="wrap_content"
    102             android:hint="请输入支付方式"
    103             android:maxLines="1"
    104             android:scrollbars="vertical"
    105             />
    106     </LinearLayout>
    107     <LinearLayout
    108         android:id="@+id/ld_btn"
    109         android:layout_width="match_parent"
    110         android:layout_height="wrap_content"
    111         android:layout_centerVertical="true"
    112         android:orientation="horizontal">
    113 
    114         <Button
    115             android:id="@+id/btn_add"
    116             android:layout_width="wrap_content"
    117             android:layout_height="wrap_content"
    118             android:layout_weight="1"
    119             android:textSize="20sp"
    120             android:text="添加" />
    121 
    122         <Button
    123             android:id="@+id/btn_update"
    124             android:layout_width="wrap_content"
    125             android:layout_height="wrap_content"
    126             android:layout_weight="1"
    127             android:textSize="20sp"
    128             android:text="修改" />
    129 
    130         <Button
    131             android:id="@+id/btn_query"
    132             android:layout_width="wrap_content"
    133             android:layout_height="wrap_content"
    134             android:textSize="20sp"
    135             android:layout_weight="1"
    136             android:text="查询" />
    137 
    138         <Button
    139             android:id="@+id/btn_delete"
    140             android:layout_width="wrap_content"
    141             android:layout_height="wrap_content"
    142             android:layout_weight="1"
    143             android:textSize="20sp"
    144             android:text="删除" />
    145     </LinearLayout>
    146 
    147     <TextView
    148         android:id="@+id/ld_show"
    149         android:layout_width="match_parent"
    150         android:layout_height="wrap_content"
    151         android:layout_marginTop="25dp"
    152         android:layout_below="@+id/ld_btn"
    153         android:textSize="20sp" />
    154 
    155 </LinearLayout>

    截图展示:

    记账:

        

    修改账单信息:

     

     删除账单信息:

     

       

  • 相关阅读:
    利用Windows消息循环,使窗体不能改变大小
    重磅发布全总结丨一文看懂阿里云弹性计算年度峰会
    阿里云弹性计算首席架构师分享云上应用架构演进三大方向
    只需5步!在轻量应用服务器部署Hexo博客
    阿里云手机正式公测,定义手机全新接入方式
    云服务器ECS年终特惠,老用户新购优惠低至4折
    阿里云发布CloudOps白皮书,ECS自动化运维套件新升级
    快速部署阿里云WebIDE(DevStudio)并参与开源项目开发
    抢先看! 2021阿里云弹性计算年度峰会嘉宾演讲内容提前曝光
    饿了么资深架构师分享云上基础架构演进
  • 原文地址:https://www.cnblogs.com/znjy/p/14905471.html
Copyright © 2011-2022 走看看