1

シナリオは次のとおりです。

ユーザーがチーム名を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);
            }
        }
    });

}
4

3 に答える 3

2

あなたはおそらくこのようなことをするべきです

final EditText editText = (EditText) findViewById(R.id.editText1);
final TextView textView = (TextView) findViewById(R.id.textView1);

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            textView.setText("changed");
        }
    }
});

もちろん、editText1とtextView1は恐ろしい変数名であることを知っています。

于 2013-02-11T22:17:16.450 に答える
2

基本的に、ボタンがクリックされたとき、またはテキストを動的に変更するために何かを入力したときに、それを行う方法が必要です。どちらも非常に単純です。これが両方の方法です

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

final TextView team1NameScore = (TextView) findViewById(R.id.team1NameScore);
final EditText team1Name = (EditText) findViewById(R.id.team1Name);
Button btn1 =(Button) findViewById(R.id.button1);

btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = team1Name.getText().toString();
            team1NameScore.setText(text);

        }
    });



team1Name.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }// do nothing

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        } // do nothing

        @Override
        public void afterTextChanged(Editable s) {
            String text = team1Name.getText().toString();
            team1NameScore.setText(text);
        }
    });
}}
于 2013-02-11T22:33:07.723 に答える
1

メンバー変数をfinalにするべきではなく、OnCreateメソッドで初期化する必要があります。チーム名の変更をリッスンしているteamNameChange(View v)を呼び出す必要があります。

public class Main extends Activity {
private EditText editText;
private TextView textView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    team1NameScore = (TextView) findViewById(R.id.team1NameScore);
    team1Name = (EditText) findViewById(R.id.team1Name);
    team1Name.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                /*team1NameScore.setText("changed");*/
                teamNameChange(v);
            }
        }
    });

}
于 2013-02-11T23:02:36.700 に答える