二月五日
记录页面的支出模块
package com.zhen.accountbook.frag_record; import android.inputmethodservice.KeyboardView; import android.os.Bundle; import android.text.TextUtils; import android.widget.*; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.zhen.accountbook.R; import com.zhen.accountbook.db.AccountBean; import com.zhen.accountbook.db.DBManager; import com.zhen.accountbook.db.TypeBean; import com.zhen.accountbook.utils.BeiZhuDialog; import com.zhen.accountbook.utils.KeyBoardUtils; import com.zhen.accountbook.utils.SelectTimeDialog; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 记录页面当中的支出模块 */ public class InFragment extends Fragment implements View.OnClickListener { KeyboardView keyboardView; EditText moneyEt; ImageView typeIV; TextView typeTv, beizhuTv, timeTv; GridView typeGv; List<TypeBean> typeList; TypeBaseAdapter adapter; AccountBean accountBean;//将需要插入到记账本的数据保存成对象的格式 @Override public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); accountBean = new AccountBean(); accountBean.setTypename("其他"); accountBean.setsImageId(R.mipmap.in_qt_fs); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_outcome, container, false); initView(view); setInitTime(); //给GridView填充数据 loadDataToGV(); setGVListener();//设置GridView每一项的点击事件 return view; } //获取当前时间,显示在timeTv上 private void setInitTime() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); timeTv.setText(sdf.format(date)); accountBean.setTime(sdf.format(date)); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); accountBean.setYear(year); accountBean.setMonth(month); accountBean.setDay(day); } private void setGVListener() { typeGv.setOnItemClickListener((adapterView, view, i, l) -> { adapter.selectPos = i; adapter.notifyDataSetChanged(); TypeBean typeBean = typeList.get(i); //点击每一项时,上边会刷新所选中的图像 String typename = typeBean.getTypename(); typeTv.setText(typename); accountBean.setTypename(typename); int sImageId = typeBean.getSImageId(); typeIV.setImageResource(sImageId); accountBean.setsImageId(sImageId); }); } public void loadDataToGV() { typeList = new ArrayList<>(); adapter = new TypeBaseAdapter(getContext(), typeList); typeGv.setAdapter(adapter); //获取数据库当中的数据源 List<TypeBean> inlist = DBManager.getTypeList(1); typeList.addAll(inlist); adapter.notifyDataSetChanged(); typeTv.setText("其他"); typeIV.setImageResource(R.mipmap.in_qt_fs); } private void initView(View view) { keyboardView = view.findViewById(R.id.frag_record_keyboard); moneyEt = view.findViewById(R.id.frag_record_et_money); typeIV = view.findViewById(R.id.frag_record_iv); typeGv = view.findViewById(R.id.frag_record_gv); typeTv = view.findViewById(R.id.frag_record_tv_type); beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu); timeTv = view.findViewById(R.id.frag_record_tv_time); //显示自定义键盘 KeyBoardUtils boardUtils = new KeyBoardUtils(keyboardView, moneyEt); boardUtils.showKeyBoard(); //设置接口,监听确定按钮 被点击 boardUtils.setOnEnsureListener(() -> { // 获取输入钱数 String moneyStr = moneyEt.getText().toString(); if (TextUtils.isEmpty(moneyStr) || moneyStr.equals("0")) { getActivity().finish(); return; } float money = Float.parseFloat(moneyStr); accountBean.setMoney(money); //获取记录的信息,将信息保存到数据库中 saveAccountToDB(); //返回上一级页面 getActivity().finish(); }); } private void saveAccountToDB() { accountBean.setKind(1); DBManager.insertItemToAccounttb(accountBean); } @Override public void onClick(View v) { if (v.getId() == R.id.frag_record_tv_beizhu) { showBZDialog(); } else if (v.getId() == R.id.frag_record_tv_time) { showTimeDialog(); } //跳转到时间选择界面 } //弹出显示时间的对话框 private void showTimeDialog() { SelectTimeDialog dialog = new SelectTimeDialog(getContext()); dialog.show(); //设定确定按钮被点击的监听器 dialog.setOnEnsureListener((time, year, month, day) -> { timeTv.setText(time); accountBean.setYear(year); accountBean.setMonth(month); accountBean.setDay(day); }); } //弹出备注对话框 public void showBZDialog() { BeiZhuDialog dialog = new BeiZhuDialog(getContext()); dialog.show(); dialog.setDialogSize(); dialog.setOnEnsureListener(() -> { String msg = dialog.getEditText(); if (!TextUtils.isEmpty(msg)) { beizhuTv.setText(msg); accountBean.setBeizhu(msg); } dialog.cancel(); }); } }