sendMessage (View view) detailed

Respond to the click event of the send button

Generally, we listen for click events in java code, but we can also associate click events directly through button properties: android:onClick 

 
 
  1. <Button
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="@string/button_send"
  5. android:onClick="sendMessage"/>

The value "sendMessage" of android:onClick is the sendMessage() method in the activity that is called when the button is clicked.

打开src目录中的MainActivity文件,在类中添加如下方法:

 
 
  1. /** Called when the user clicks the Send button */
  2. publicvoid sendMessage(View view){
  3. // Do something in response to button
  4. }

Since there is a View class in the parameter of the method, you need to import the corresponding package

 
 
  1. import android.view.View;


In order for the system to automatically call the sendMessage method when the button is clicked, your method must adhere to the following conventions:

1. Must be a public method;

2. Must be a void return type;

3. Must have a View parameter (representing which View is clicked);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325791248&siteId=291194637