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

好吧,您基本上必须在单击按钮或键入内容以动态更改文本时进行操作,这两者都非常简单。这是两种方法

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 回答
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 回答
1

你不应该让你的成员变量是最终的,它们应该在 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 回答