java-在EditText上使用TextWatcher在android中实时计算总数和总和?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在EditText上使用TextWatcher在android中实时计算总数和总和?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5408字,纯文字阅读大概需要8分钟。
内容图文
![java-在EditText上使用TextWatcher在android中实时计算总数和总和?](/upload/InfoBanner/zyjiaocheng/664/1a997d34d4fe455f90b5113fa78a62c2.jpg)
在这里,我想使用来自数据库和setText的默认值作为该值,并计算净费率和总计,否则,如果用户编辑费率或收取费用,我想根据该值计算净费率和总计即时的.
这是我用于计算和显示值的代码.
private void showItem(String json) {
String itembarcode = "";
String itemdesc = "";
String weight = "";
String rate = "";
String making = "";
double wt = 0.0d;
double rt = 0.0d;
double mk = 0.0d;
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(ParseBarcode.JSON_ARRAY);
JSONObject itemData = result.getJSONObject(0);
itembarcode = itemData.getString(ParseBarcode.KEY_BARCODE);
itemdesc = itemData.getString(ParseBarcode.KEY_DESC);
weight = itemData.getString(ParseBarcode.KEY_WEIGHT);
rate = itemData.getString(ParseBarcode.KEY_RATE);
making = itemData.getString(ParseBarcode.KEY_MAKING);
} catch (JSONException e) {
e.printStackTrace();
}
//table started
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
rowParams.setMargins(16, 0, 16, 0);
TableLayout tableLayout = new TableLayout(AddInvEst.this);
tableLayout.setLayoutParams(tableParams);
TableRow newRow = new TableRow(AddInvEst.this);
newRow.setLayoutParams(tableParams);
TextView barCode = new TextView(AddInvEst.this);
barCode.setLayoutParams(rowParams);
barCode.setGravity(Gravity.CENTER);
TextView itemDesc = new TextView(AddInvEst.this);
itemDesc.setLayoutParams(rowParams);
itemDesc.setGravity(Gravity.CENTER);
TextView weightLine = new TextView(AddInvEst.this);
weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));
weightLine.setGravity(Gravity.CENTER);
EditText rateAmount = new EditText(AddInvEst.this);
rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
rateAmount.setGravity(Gravity.CENTER);
// String rtAmt = rateAmount.getText().toString().trim();
EditText makingAmount = new EditText(AddInvEst.this);
makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
makingAmount.setGravity(Gravity.CENTER);
//String mkAmt = makingAmount.getText().toString().trim();
TextView netRate = new TextView(AddInvEst.this);
netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
netRate.setGravity(Gravity.CENTER);
TextView itemtotal = new TextView(AddInvEst.this);
itemtotal.setLayoutParams(rowParams);
itemtotal.setGravity(Gravity.CENTER);
wt = Double.parseDouble(weight);
rt = Double.parseDouble(rate);
mk = Double.parseDouble(making);
double NetRate = rt + mk;
double Total = (NetRate / 10) * wt;
barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(NetRate + "");
itemtotal.setText(Total + "");
newRow.addView(barCode);
newRow.addView(itemDesc);
newRow.addView(weightLine);
newRow.addView(rateAmount);
newRow.addView(makingAmount);
newRow.addView(netRate);
newRow.addView(itemtotal);
itemTable.addView(newRow);
}
Using this code I am able to calculate the net rate and total but i want to round the double value to 4 decimal points.Also how would i calculate the sum total of all the totals in real time.Any help is appreciated.Thanks in advance.
This is my xml code.
解决方法:
以下不是完整的代码,但是可以给出实现的思路.
您必须创建一种新的方法来计算和显示结果-
private void calculateAndShow(long wt, long rt, long mk){
double NetRate = rt + mk;
double Total = (NetRate / 10) * wt;
// Change the value here
barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(NetRate + "");
itemtotal.setText(Total + "");
}
现在,将这些行添加到您的方法之外-
private TextWatcher rateTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String rate = rateAmount.getText().toString();
newRate = Double.parseDouble(rate);
calculateAndShow(wt, newRate, mk);
}
};
private TextWatcher makingAmountTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String makingAmount = makingAmount.getText().toString();
newMK = Double.parseDouble(makingAmount);
calculateAndShow(wt, rt, newMK);
}
};
这些textwatcher就是这样的您的edittext-
EditText rateAmount = new EditText(AddInvEst.this);
rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
rateAmount.setGravity(Gravity.CENTER);
rateAmount.addTextChangedListener(rateTextWatcher);
EditText makingAmount = new EditText(AddInvEst.this);
makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
makingAmount.setGravity(Gravity.CENTER);
makingAmount.addTextChangedListener(makingAmountTextWatcher);
内容总结
以上是互联网集市为您收集整理的java-在EditText上使用TextWatcher在android中实时计算总数和总和?全部内容,希望文章能够帮你解决java-在EditText上使用TextWatcher在android中实时计算总数和总和?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。