TextView不在用户输入上显示EditText字符串

时间:2013-02-11 22:05:44

标签: android textview gettext tostring

见下面的情景:

用户在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);
            }
        }
    });

}

3 个答案:

答案 0 :(得分: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是可怕的变量名。

答案 1 :(得分: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);
        }
    });
}}

答案 2 :(得分: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);
            }
        }
    });

}