react native输入框 输入金额等数据后如何格式化显示
2016-11-25 15:35
1391 查看
import React ,{ Component } from 'react'; import {View} from 'react-native'; import { Cells, Cell, CellHeader, CellBody, CellFooter, CellText, Input, Label, } from 'rn-weui/src'; export class InputBox extends Component{ constructor(props) { super(props); this.state = { money:'', endEdit:true }; this.fmoney = this.fmoney.bind(this); } /** * 格式化money * s为要格式化的money * n为小数位数 */ fmoney(s, n){ if(s==='') return; n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1]; var t = ""; for(let i = 0; i < l.length; i ++ ) { t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); } return t.split("").reverse().join("") + "." + r; } render(){ return ( <Cells> <Cell> <CellHeader><Label>单价</Label></CellHeader> <CellBody> <View> <Input style={{height:40}} underlineColorAndroid="transparent" placeholder="请输入金额" keyboardType='numeric' value={this.state.endEdit?this.fmoney(this.state.money,2):this.state.money+''} onChangeText={(text)=>{ this.setState({money:text}); }} onFocus={()=>{ // this.setState({endEdit:false}); this.setState({endEdit:false,money:''}); }} onEndEditing={(event) => { this.setState({endEdit:true}); }} ></Input> </View> </CellBody> <CellFooter>元</CellFooter> </Cell> <Cell> <CellHeader><Label>单价</Label></CellHeader> <CellBody> <View> <Input style={{height:40}} underlineColorAndroid="transparent" placeholder="请输入物品单价" keyboardType='numeric' ></Input> </View> </CellBody> <CellFooter>元</CellFooter> </Cell> </Cells> ); } }
首先,state声明一个输入框是否正在输入的状态endEdit和一个用来表示输入框中显示的值money; 然后,在onblue获得焦点的实践中,将是否正在输入的状态改为false,输入框中的显示money;输入完成,将endEdit改为出,输入框中的值改为格式化后的值。
相关文章推荐
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- asp累加如何将总数量、总金额显示在某日最后一条数据的备注栏?
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(七)----使用Game ID避免数据重复输入
- EasyUI numbox输入框,金额格式化显示
- 请问 Wallpaper Calendar 是如何实现在桌面显示日历和输入数据的????
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- vue 输入框 限定输入金额的范围并且用千分符显示,初始值有格式限制
- PHP - 如何在HTML中格式化显示JSON数据
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- 如何在JPG或BMP图片上显示输入的订单数据内容,并在报表打印时显示出来,后台数据库是SQL SERVER 2000 ,先谢了.高分!
- 将数据源的数据格式化显示,加上金额符号
- 输入小写金额同时显示大写金额
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?
- 如何让DataGrid控件不换行 数据显示不完全后面加"..."
- (原创)如何让web页面产生服务器数据返回后仍然能够保留到用户输入的位置!
- 如何点击按钮弹出新窗口,输入数据后返回并刷新页面?(转自孟子E章)
- 如何使用 ASP.NET查询和显示 Excel 数据