zoukankan      html  css  js  c++  java
  • 2021.3.8

    在Java里新建项目,命名为new_cost.java:

    package com.example.myaccountapp;

    import androidx.appcompat.app.AppCompatActivity;

    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.Toast;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class new_cost extends AppCompatActivity {
    private DBHelper helper;
    private EditText et_cost_title;
    private EditText et_cost_money;
    private DatePicker dp_cost_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_cost);
    initView();
    }

    private void initView() {
    helper = new DBHelper(new_cost.this);
    et_cost_title = findViewById(R.id.et_cost_title);
    et_cost_money = findViewById(R.id.et_cost_money);
    dp_cost_date = findViewById(R.id.dp_cost_date);

    }


    public void okButton(View view) {
    String titleStr = et_cost_title.getText().toString().trim();
    String moneyStr = et_cost_money.getText().toString().trim();
    String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-"
    + dp_cost_date.getDayOfMonth();//这里getMonth会比当前月份少一个月,所以要+1
    if ("".equals(moneyStr)) {//可以不填写Title但是不能不填金额
    Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
    } else {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("Title", titleStr);
    values.put("Money", moneyStr);
    values.put("Date", dateStr);
    long account = db.insert("account", null, values);
    if (account > 0) {
    Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
    setResult(1);
    finish();
    } else {
    Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
    db.close();
    }
    setResult(1);
    finish();
    }

    }
    }

  • 相关阅读:
    在opencv3中实现机器学习之:利用正态贝叶斯分类
    在opencv3中进行图片人脸检测
    在opencv3中利用SVM进行图像目标检测和分类
    在opencv3中实现机器学习之:利用svm(支持向量机)分类
    在matlab和opencv中分别实现稀疏表示
    opencv2学习:计算协方差矩阵
    用python简单处理图片(3):添加水印
    Google protocol buffer在windows下的编译
    GEOS库学习之五:与GDAL/OGR结合使用
    GEOS库学习之四:几何关系判断
  • 原文地址:https://www.cnblogs.com/SirNie/p/14911008.html
Copyright © 2011-2022 走看看