多个 edittext 只有一个 textwatchers 用于 android 中的计算
multiple edittext with only one textwatchers for calculation in android
我将从第一个 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;
}
}
};
您可以使用此方法并调整您的代码
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
我将从第一个 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;
}
}
};
您可以使用此方法并调整您的代码
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