Why we use String message = editText.getText().toS

2019-02-11 09:49发布

I am new to android development. I am learning android development given here their is a piece of code

Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);

I want to know why we use String message = editText.getText().toString() after using EditText editText = (EditText) findViewById(R.id.edit_message); What does EditText return?

7条回答
smile是对你的礼貌
2楼-- · 2019-02-11 10:24

Using EditText editText = (EditText) findViewById(R.id.edit_message);

This line registers your xml editText with the class file editText. As you develop an app, the design is done in the xml file and coding is in the .class file.

Somewhere it is working as a Listener, like if you write something in the editText then,

Using EditText editText = (EditText) findViewById(R.id.edit_message); 

By this line it is notified that the editText is notified.

NOTE: If you do directly String message = editText.getText().toString().without doing this
EditText editText = (EditText) findViewById(R.id.edit_message) you will get NullPointer Exception.

查看更多
登录 后发表回答