シナリオは次のとおりです。
ユーザーがチーム名をEditText
フィールドに入力すると、そのテキストがTextViewの新しい名前になります(を使用setText()
)。ユーザーがEditText
まだフィールドに何も入力していない場合、setText()
はxmlファイルで宣言されているのと同じテキストプロパティになります(基本的にはデフォルトです)。
これまでのところ、私はこのコードを持っていますが、機能していません。フィールドに何かを入力EditText
して他の何かをクリックしても、TextView
は変わりません。
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);
}
}
テキストが変更されない理由はありますか?
編集-最終作業コード:
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);
}
}
});
}