Android 如何在textwatcher中使用多个EditText进行gst计算
我的活动中有多(3)篇编辑文本 我想为其中的文本更改时执行一个操作。我是否需要有7个不同的文本观察者,或者有没有一种方法可以将同一个文本观察者用于或进行某种切换?Android 如何在textwatcher中使用多个EditText进行gst计算,android,textwatcher,Android,Textwatcher,我的活动中有多(3)篇编辑文本 我想为其中的文本更改时执行一个操作。我是否需要有7个不同的文本观察者,或者有没有一种方法可以将同一个文本观察者用于或进行某种切换? 我尝试过这样的东西。我现在是新来的,所以知道的不多 请给我详细解释一下 public void sellTextViewCalInt(){ s_mrp_value = (TextView) findViewById(R.id.s_mrp_value); s_rate_value = (TextView) fin
我尝试过这样的东西。我现在是新来的,所以知道的不多 请给我详细解释一下
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\u sale\u rate.addTextChangedListener(此)
text\u sale\u折扣。addTextChangedListener(此)
text\u sale\u tax.addTextChangedListener(此)
试试这个
活动或片段应实现TextWatcher
text\u sale\u rate.addTextChangedListener(此)
text\u sale\u折扣。addTextChangedListener(此)
text\u sale\u tax.addTextChangedListener(此)
afterTextChanged:if else
if s==text\u sale\u rate.getEditableText()等给出逻辑,我现在是新来的,所以不知道太多,请详细解释。请给我一个完整的例子,并请非常了解AftertextChanged:如果s==text\u sale\u rate.getEditableText()等,请给出逻辑if elseif s==text。请给我一个完整的示例,并请非常理解显示的错误,如try getEditableText()而不是getEditable()。此getEditableText()和getEditable()都显示错误。显示的错误为try getEditableText()而不是getEditable()。此getEditableText()和getEditable()都显示错误
@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
}
}