实战演练-记账本App (五)

在使用了记账本APP后发现还存在这一些问题

1、用户添加账单时如果内容、金额输入为空时不会给出提示,会造成下面这种情况,添加账单后信息显示为空,降低了使用体验

 

 改动的地方在CostActivity.java文件中,只要添加判断为空的语句即可,代码如下

//添加
        Button btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(CostActivity.this);
                LayoutInflater inflater = LayoutInflater.from(CostActivity.this);
                View viewDialog = inflater.inflate(R.layout.new_data, null);
                final EditText title = (EditText) viewDialog.findViewById(R.id.et_cost_title);
                final EditText money = (EditText) viewDialog.findViewById(R.id.et_cost_money);
                final DatePicker date = (DatePicker) viewDialog.findViewById(R.id.dp_cost_date);
                builder.setView(viewDialog);
                builder.setTitle("新收入");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CostBean costBean = new CostBean();
                        costBean.costTitle = title.getText().toString();
                        costBean.costMoney = money.getText().toString();
                        costBean.costDate = date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth();
                        if ( TextUtils.isEmpty(costBean.costTitle) || TextUtils.isEmpty(costBean.costMoney) ) {
                            Toast.makeText(view.getContext(), "请将信息填写完整", Toast.LENGTH_SHORT).show();
                        } else {
                            mDatabaseHelper.insertCost(costBean);
                            Toast.makeText(view.getContext(), "添加成功", Toast.LENGTH_SHORT).show();
                            mCostBeanList.add(costBean);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
            }
        });
    }

在IncomeActivity.java文件中也是相同的改法

 

 

当输入为空或不完整时会给出提示。

 

 

2、用户无法进行单一删除

 改动的地方在CostActivity.java文件中,添加长按触发事件进行删除即可,代码如下

 //长按删除
        costList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder b = new AlertDialog.Builder(CostActivity.this);
                //设置提示框内容
                b.setMessage("确认删除?");
                //设置标题栏
                b.setTitle("提示");
                b.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        mCostBeanList.remove(position);
                        mAdapter.notifyDataSetChanged();
                        Toast.makeText(CostActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(CostActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                b.create().show();
                return true;
            }
            });

长按某一账单后弹出提示框

 

 在IncomeActivity.java文件中也是相同的方法。

posted @ 2020-01-26 14:40  MoooJL  阅读(227)  评论(0编辑  收藏  举报