写一个带有记账本的微信小程序并给出代码
时间: 2024-05-11 14:19:46 浏览: 164
微信小程序项目实例-记账本
5星 · 资源好评率100%
以下是一个简单的带有记账本功能的微信小程序代码:
index.wxml:
```
<view class="container">
<view class="header">我的账本</view>
<view class="balance">余额:{{balance}}</view>
<view class="form">
<input placeholder="请输入金额" bindinput="inputChange" value="{{inputValue}}" />
<button bindtap="spendMoney">支出</button>
<button bindtap="earnMoney">收入</button>
</view>
<view class="list">
<view wx:for="{{recordList}}" wx:key="{{index}}">
<view class="item {{item.type}}">{{item.type}}:{{item.money}}元</view>
</view>
</view>
</view>
```
index.js:
```
Page({
data: {
balance: 0,
inputValue: '',
recordList: []
},
inputChange: function(e) {
this.setData({
inputValue: e.detail.value
})
},
spendMoney: function() {
if (this.data.inputValue === '') {
return;
}
var recordList = this.data.recordList;
recordList.push({
type: '支出',
money: this.data.inputValue
});
var balance = this.data.balance - parseFloat(this.data.inputValue);
this.setData({
balance: balance.toFixed(2),
inputValue: '',
recordList: recordList
});
},
earnMoney: function() {
if (this.data.inputValue === '') {
return;
}
var recordList = this.data.recordList;
recordList.push({
type: '收入',
money: this.data.inputValue
});
var balance = this.data.balance + parseFloat(this.data.inputValue);
this.setData({
balance: balance.toFixed(2),
inputValue: '',
recordList: recordList
});
}
})
```
index.wxss:
```
.container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 30px;
}
.header {
font-size: 24px;
font-weight: bold;
margin-bottom: 20px;
}
.balance {
font-size: 18px;
margin-bottom: 20px;
}
.form {
display: flex;
flex-direction: row;
align-items: center;
}
.form input {
width: 200px;
height: 40px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 0 10px;
margin-right: 10px;
}
.form button {
width: 60px;
height: 40px;
background-color: #007aff;
color: #fff;
border-radius: 4px;
border: none;
}
.list {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 30px;
}
.item {
font-size: 16px;
margin-bottom: 10px;
padding: 10px;
border-radius: 4px;
}
.item.spend {
color: red;
}
.item.earn {
color: green;
}
```
这个小程序实现了一个简单的记账本功能,用户可以在输入框中输入金额,然后选择支出或收入按钮来记录自己的花费和收入情况。同时,页面中会显示当前的余额和历史记录列表。
阅读全文