如何不使用EditText
而是仅使用TextWatcher的Button
方法求和多个onTextChanged()
的总数。假设这是我的布局:
EditText1 = 5
EditText2 = 5
EditText3 = 5
EditText4 = 5
总数= 20
等等
并获得总数? EditText
的值更改时,总数应为CHANGING。
我已阅读this答案。但是我无法在程序上很好地执行它。
这是我的布局
最佳答案
您需要使用TextWatcher()
而不是使用5个TextWatcher()
,您可以仅使用一个TextWatcher()
对其进行管理
尝试这个
如果您的所有四个四个edittext
不为空,请使用以下答案,它将计算editext
值的总和
public class MainActivity extends AppCompatActivity {
EditText edtOne, edtTwo, edtThree, edtFour;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtOne = findViewById(R.id.edtOne);
edtTwo = findViewById(R.id.edtTwo);
edtThree = findViewById(R.id.edtThree);
edtFour = findViewById(R.id.edtFour);
tvResult = findViewById(R.id.tvResult);
edtOne.addTextChangedListener(textWatcher);
edtTwo.addTextChangedListener(textWatcher);
edtThree.addTextChangedListener(textWatcher);
edtFour.addTextChangedListener(textWatcher);
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!TextUtils.isEmpty(edtOne.getText().toString().trim())
|| !TextUtils.isEmpty(edtTwo.getText().toString().trim())
|| !TextUtils.isEmpty(edtThree.getText().toString().trim())
|| !TextUtils.isEmpty(edtFour.getText().toString().trim())
) {
int answer = Integer.parseInt(edtOne.getText().toString().trim()) +
Integer.parseInt(edtTwo.getText().toString().trim()) +
Integer.parseInt(edtThree.getText().toString().trim()) +
Integer.parseInt(edtFour.getText().toString().trim());
Log.e("RESULT", String.valueOf(answer));
tvResult.setText(String.valueOf(answer));
}else {
tvResult.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
}
更新
如果您要计算所有
editext
的值,即使您的editext为空,也请尝试在TextWatcher()
以下TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!TextUtils.isEmpty(edtOne.getText().toString().trim())
|| !TextUtils.isEmpty(edtTwo.getText().toString().trim())
|| !TextUtils.isEmpty(edtThree.getText().toString().trim())
|| !TextUtils.isEmpty(edtFour.getText().toString().trim())
) {
int firtValue = TextUtils.isEmpty(edtOne.getText().toString().trim()) ? 0 : Integer.parseInt(edtOne.getText().toString().trim());
int secondValue = TextUtils.isEmpty(edtTwo.getText().toString().trim()) ? 0 : Integer.parseInt(edtTwo.getText().toString().trim());
int thirdValue = TextUtils.isEmpty(edtThree.getText().toString().trim()) ? 0 : Integer.parseInt(edtThree.getText().toString().trim());
int forthValue = TextUtils.isEmpty(edtFour.getText().toString().trim()) ? 0 : Integer.parseInt(edtFour.getText().toString().trim());
int answer = firtValue + secondValue + thirdValue + forthValue;
Log.e("RESULT", String.valueOf(answer));
tvResult.setText(String.valueOf(answer));
}else {
tvResult.setText("");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
关于java - 在多个EditText上添加值,并在每个textchange上获取总计,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54361725/