Android:onClick attribute

Responding to the click event of the send button
Generally , we listen for the click event in the java code, but we can also directly associate the click event through the android:onClick attribute of the button:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />

The value "sendMessage" of android:onClick is the sendMessage() method in the activity that is called when the button is clicked.
Open the MainActivity file in the src directory and add the following method to the class:
/ * Called when the user clicks the Send button /
public void sendMessage(View view) {
// Do something in response to button
}
Since there is Vie in the parameter of the method Therefore, you need to introduce the corresponding package
import android.view.View;
//Key
In order for the system to automatically call the sendMessage method when the button is clicked, your method must comply with the following conventions:
1. Must be a public method;
2. Must be 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=325811585&siteId=291194637