Four ways to write activity jump button events in Android

Specific implementation code:

In the above code:

1. The mainactivity class we created needs to inherit from activity

2. You need to override the oncreate method and load the corresponding layout file through the setcontentview method

3. Find the corresponding control (the control defined in the layout file) through the findviewbyid method and bind a click event (implemented by listener in Java and by delegate in c#)

4. You can pass data through the intent object and jump to other activities

5. Oncreateoptions menu and onoptionstitemselected are the methods when adding and selecting menu items.

The following are the contents of the four activities:

First:

Second:

Third:

Fourth:

You need to specify the btnclickevent method in the layout file XML.

Among the above four writing methods, the third method is relatively more used. When multiple buttons in an activity need to trigger the click event, it is easier to manage and maintain the button event code through the third writing method.

Layout is a very important piece of content. I will explain it in the following blog, which is briefly mentioned here.

We use linear layout (linear layout, other relative layout, absolute layout, etc.), set the Android: orientation property value to vertical, and display the controls from top to bottom.

The other three layout files are the same as this content, with only one button placed.

activity_ main. XML is configured as follows (four buttons are simply placed):

Finally, a more important step needs to be taken in Android manifest Configure and register the activity in the XML file. The complete configuration is as follows:

There's one thing to pay attention to,

Set mainactivity to "main activity", that is, which activity is displayed first when starting.

The following activities need to be registered in the "manifest file" so that they can be found in the program.

strings. Contents of XML file configuration:

Of course, you can also write directly in the layout file, but this is more conducive to maintenance, which is also the recommended method for Android development.

The above is the four writing methods of activity jump button event in Android introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>