Heres the scenario:
A user enters a team name into an EditText field, the text of that is then the new name of a TextView (using setText()). If the user has not entered anything into the EditText field yet, the setText() will be the same text property, as declared in the xml file (so basically the default).
So far I have this code and it is not working. When I type anything into the EditText field and click on something else, the TextView does not change.
public void teamNameChange(View view)
{
TextView team1NameScore = (TextView) findViewById(R.id.team1NameScore);
EditText team1Name = (EditText) findViewById(R.id.team1Name);
if (team1Name.getText().toString().isEmpty())
{
team1NameScore.setText("Team 1 Score:");
} else {
team1NameScore.setText("" +team1Name);
}
}
Any idea why the text isn’t changing?
EDIT- FINAL WORKING CODE:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView team1NameScore = (TextView) findViewById(R.id.team1NameScore);
final EditText team1Name = (EditText) findViewById(R.id.team1Name);
team1Name.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String newString = team1Name.getText().toString();
if (!hasFocus) {
team1NameScore.setText("" + newString);
}
}
});
}
You should probably do something like this
Knowing that, of course, editText1 & textView1 are horrible variable names.