且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我们在使用EditText之后使用String message = editText.getText().toString()?

更新时间:2023-11-26 21:04:58

使用EditText editText =(EditText)findViewById(R.id.edit_message);

此行将您的xml editText注册到类文件editText.在开发应用程序时,设计是在xml文件中完成的,编码是在.class文件中的.

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.

它在某些地方充当侦听器,例如,如果您在editText中编写了一些内容,

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); 

通过此行,通知editText被通知.

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

注意:如果您直接执行String message = editText.getText().toString().不执行此操作
EditText editText = (EditText) findViewById(R.id.edit_message)您将获得NullPointer异常.

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.