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();
    }

    }
    }

  • 相关阅读:
    android xml 布局错误
    java int与integer的区别
    android html.fromHtml 用例
    Android 手势操作识别
    android android 判断是否滑动
    Android 通过 Intent 传递类对象或list对象
    android 学习JSON
    android 解决ListView点击与滑动事件冲突
    关于android的日志输出&LogCat
    android ListView 属性
  • 原文地址:https://www.cnblogs.com/SirNie/p/14911008.html
Copyright © 2011-2022 走看看