【发布时间】:2019-08-06 19:26:04
【问题描述】:
我的活动中有多个 (3) 个 EditText。
我想在其中的文本发生更改时执行操作。我需要有 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());
}
};
【问题讨论】:
-
afterTextChanged: 给出逻辑 if else
if s == text_sale_rate.getEditableText() etc -
我是新人所以不太了解请详细解释一下。请给我一个完整的例子,请多多理解
标签: android textwatcher