使用EditText中存储的文本更新TextView

时间:2011-10-27 19:34:22

标签: android android-edittext textview

我是一名Java新手,需要我的代码帮助。

我正在尝试从下面的代码中获取变量,以便稍后我可以将您在EditText字段中编写的数字显示为TextView

代码:

public class MoneyTickerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText edittext = (EditText) findViewById(R.id.editText1);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  Toast.makeText(MoneyTickerActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                  return true;
                }
                return false;
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

// get text view
final TextView texView = (TextView) findViewById(R.id.textView1);

而不是

Toast.makeText(MoneyTickerActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();

textView.setText(edittext.getText());