Reputation: 97
I have two EdiText and when I write a number in the first one I would need the second to be set to 162 - first one. If it is necessary to re-type a second number, the component should recalculate the first number.
If I write something in the second the first must behave exactly like the second.
Below there is my code but it does not work:
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
View.OnClickListener inputScoreListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
int inputScoreWeInteger = Integer.parseInt(inputScoreWe.getText().toString());
int inputScoreYouInteger = Integer.parseInt(inputScoreYou.getText().toString());
if (inputScoreWeInteger > 0) {
inputScoreYouInteger = 162 - inputScoreWeInteger;
} else if (inputScoreYouInteger > 0) {
inputScoreWeInteger = 162 - inputScoreYouInteger;
}
String s1 = inputScoreWeInteger + "";
String s2 = inputScoreYouInteger + "";
inputScoreWe.setText(s1);
inputScoreYou.setText(s2);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
};
inputScoreWe.setOnClickListener(inputScoreListener);
inputScoreYou.setOnClickListener(inputScoreListener);
Upvotes: 0
Views: 103
Reputation: 725
try this
brandET = findViewById(R.id.addCar_brand);
modelET = findViewById(R.id.addCar_model);
brandET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
brandChange = hasFocus;
}
});
modelET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
modelChange = hasFocus;
}
});
brandET.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) {
if (brandChange && count > 0) {
int dataFromBrand = Integer.parseInt(s.toString());
modelET.setText((162 - dataFromBrand) + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
modelET.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) {
if (modelChange && count > 0) {
int dataFromModel = Integer.parseInt(s.toString());
brandET.setText((162 - dataFromModel) + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
here brandET and modelET is your two edit text...and...brandChange and modelChange are two global boolean data
Upvotes: 0
Reputation: 1422
replace your code with code as below
inputScoreWe = findViewById(R.id.inputScoreWe);
inputScoreYou = findViewById(R.id.inputScoreYou);
inputScoreWe.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) {
if (s.length() > 0) {
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString()) + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputScoreYou.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) {
if (s.length() > 0) {
inputScoreWe.setText(162 - Integer.parseInt(inputScoreWe.getText().toString()) + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 1
Reputation: 12953
Use text change listener to trigger for everytime you change value
inputScoreWe.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) {
if(s.length() > 0){
inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString())+"");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 1