android - 如何在 textwatcher 中使用多个 EditText 进行 gst 计算
问题描述
我的活动中有多个 (3) EditTexts。
我想在它们中的文本发生变化时采取行动。我需要有 7 个不同的 TextWatchers 还是有办法使用相同的一个或做某种切换?我试过这样的东西。我现在是新手,所以不太了解请详细解释我
public void sellTextViewCalInt(){
s_mrp_value = (TextView) findViewById(R.id.s_mrp_value);
s_rate_value = (TextView) findViewById(R.id.s_rate_value);
s_discount_value = (TextView) findViewById(R.id.s_discount_value);
s_taxable_value = (TextView) findViewById(R.id.s_taxable_value);
s_tax_value = (TextView) findViewById(R.id.s_tax_value);
s_price_value = (TextView) findViewById(R.id.s_price_value);
s_total_value = (TextView) findViewById(R.id.s_total_value);
}
public void sellTabelCalculateView() {
final EditText text_sale_rate = (EditText) findViewById(R.id.item_sale_rate);
final EditText text_sale_discount = (EditText) findViewById(R.id.item_sale_discount);
final EditText text_sale_tax = (EditText) findViewById(R.id.item_sale_tax);
sellTextViewCalInt();
TextWatcher textWatcher = 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 = text_sale_rate.getText().toString();
String discount = text_sale_discount.getText().toString();
String tax = text_sale_tax.getText().toString();
int sellRateInNo = Integer.valueOf(rate); // String to Integer
int sellDisInNo = Integer.valueOf(discount); // String to Integer
int sellTaxInNo = Integer.valueOf(tax); // String to Integer
s_rate_value.setText(item_sale_rate.getText().toString()); // at is String
s_discount_value.setText(text_sale_discount.getText().toString());// at is String
int taxableInNo = sellRateInNo - sellDisInNo;// calculation
s_taxable_value.setText(String.valueOf(taxableInNo)); // Integer to String
int taxInNo = (taxableInNo * sellTaxInNo) / 100 ; // calculation
s_tax_value.setText(String.valueOf(taxInNo)); // Integer to String
int priceInNo = taxableInNo + taxInNo ; // calculation
s_price_value.setText(String.valueOf(priceInNo)); // Integer to String
int mrp = sellRateInNo + taxInNo; // calculation
s_mrp_value.setText(String.valueOf(mrp)); // Integer to String
}
};
text_sale_rate.addTextChangedListener(textWatcher);
text_sale_discount.addTextChangedListener(textWatcher);
text_sale_tax.addTextChangedListener(textWatcher);
s_mrp_value.addTextChangedListener(textWatcher);
s_rate_value.addTextChangedListener(textWatcher);
s_discount_value.addTextChangedListener(textWatcher);
s_taxable_value.addTextChangedListener(textWatcher);
s_tax_value.addTextChangedListener(textWatcher);
s_price_value.addTextChangedListener(textWatcher);
s_total_value.addTextChangedListener(textWatcher);
}
如果我做这样的事情,那么计算在哪里以及我怎么能不明白呢?
private final TextWatcher rateValue = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
s_rate_value.setText(item_sale_rate.getText());
int rateInNo = new Integer(s_rate_value.getText().toString());
}
};
private final TextWatcher discountValue = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
s_discount_value.setText(item_sale_discount.getText());
int discountInNo = new Integer(s_discount_value.getText().toString());
}
};
private final TextWatcher taxValue = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
s_tax_value.setText(item_sale_tax.getText());
int taxInNo = new Integer(s_tax_value.getText().toString());
}
};
解决方案
试试这个活动或片段应该实现 TextWatcher
text_sale_rate.addTextChangedListener(this);
text_sale_discount.addTextChangedListener(this);
text_sale_tax.addTextChangedListener(this);
@Override
public void afterTextChanged(Editable s) {
if (s== text_sale_rate.getEditableText() ) {
//what should you do when text_sale change
} else if (s == text_sale_discount.getEditableText()) {
//what then
} else {
//another logic
}
}
推荐阅读
- java - java 8如何按该集合中的列表属性对集合进行分组
- sql - SQL - 数据库卡住(恢复中)
- java - 未收到android fcm通知
- html - 有溢出的伪边界突破父级:自动
- autohotkey - 我正在尝试通过自动热键更改 One Note 中的字体颜色
- python - Python Flask Gunicorn 配置说“从 flask_ckeditor 导入 CKEditor,CKEditorField ImportError:没有名为 flask_ckeditor 的模块”
- nearprotocol - 有什么方法可以检查 NEAR 协议使用的 RocksDB 实例的内容吗?
- sql - Oracle 12C SQL 聚合查询逻辑
- java - 如何将 BST 中的删除方法从递归切换为迭代?
- image - 带有图像路径的vue问题的包裹