我将从第一个EditText
获取一个值,从第二个EditText
获得另一个值,并计算两个值,并在第三个EditText
中显示结果,该结果仅使用一个TextWatcher
在android中。
请帮帮我谢谢。
public class TextWatcher_Activity extends Activity {
private EditText passwordEditText, passwordEditText1, passwordEditText2;
private TextView textView;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textwatcher);
/* Initializing views */
passwordEditText = (EditText) findViewById(R.id.password);
textView = (TextView) findViewById(R.id.passwordHint);
textView.setVisibility(View.GONE);
passwordEditText1 = (EditText) findViewById(R.id.password1);
passwordEditText2 = (EditText) findViewById(R.id.password2);
passwordEditText.addTextChangedListener(passwordWatcher);
passwordEditText1.addTextChangedListener(passwordWatcher);
passwordEditText2.addTextChangedListener(passwordWatcher);
}
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
*//* if (s.hashCode() == passwordEditText.getText().hashCode()) {
//Do else something with input.
} else if (s.hashCode() == passwordEditText1.getText().hashCode()) {
//Do something else useful with input.
}*//*
switch (view.getId()) {
case R.id.password:
//doStuff(1);
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setText("You have entered : " + passwordEditText.getText());
}
break;
case R.id.password1:
//doStuff(2);
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setText("You have entered : " + passwordEditText1.getText());
}
break;
case R.id.password2:
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setText("You have entered : " + passwordEditText2.getText());
}
break;
}
}
};
答案 0 :(得分:0)
您可以使用此方法并调整代码
private void calculate(EditText editText1, EditText editText2, final EditText editText3) {
final int[] value1 = {0};
final int[] value2 = {0};
final int[] total = {0};
editText1.addTextChangedListener(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) {
value1[0] = Integer.parseInt(s.toString());
total[0] = value1[0] + value2[0];
editText3.setText(total[0]);
}
@Override
public void afterTextChanged(Editable s) {}
});
editText2.addTextChangedListener(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) {
value2[0] = Integer.parseInt(s.toString());
total[0] = value1[0] + value2[0];
editText3.setText(total[0]);
}
@Override
public void afterTextChanged(Editable s) {}
});
}
editText3总共有editText1 + editText2