Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Android creates and responds to menu events

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how Android creates and responds to menu events". In daily operation, I believe many people have doubts about how Android creates and responds to menu events. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how Android creates and responds to menu events". Next, please follow the editor to study!

In CrimeLab.java, add the addCrime () method to add Crime to the list, as shown in the following code.

Add a new crime (CrimeLab.java)

...

Public void addCrime (Crime c) {

MCrimes.add (c)

}

Public List getCrimes () {

Return mCrimes

}

...

Now that you can add crime records manually, there is no need for the program to automatically generate 100 crime records. In CrimeLab.java, delete the code that generates random crime records, as shown in the code in the following figure.

Code listing: goodbye, random crime record! (CrimeLab.java)

When a user clicks a menu item in the menu, fragment receives a callback request from the onOptionsItemSelected (MenuItem) method. The parameter passed in to this method is a MenuItem instance that describes the user's choice.

The current menu has only one menu item, but the menu usually contains multiple menu items. By checking the menu item ID, you can determine which menu item is selected and then respond accordingly. This ID is actually the resource ID assigned to the menu item in the menu definition file.

In CrimeListFragment.java, implement the onOptionsItemSelected (MenuItem) method to respond to the selection event of the menu item. In this method, create a new Crime instance, add it to the CrimeLab, and then start the CrimePagerActivity instance so that the user can edit the newly created Crime record, as shown in the following code.

Respond to menu item selection events (CrimeListFragment.java)

@ Override

Public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {

Super.onCreateOptionsMenu (menu, inflater)

Inflater.inflate (R.menu.fragment_crime_list, menu)

}

@ Override

Public boolean onOptionsItemSelected (MenuItem item) {

Switch (item.getItemId ()) {

Case R.id.menu_item_new_crime:

Crime crime = new Crime ()

CrimeLab.get (getActivity ()) .addkeeper (crime)

Intent intent = CrimePagerActivity

.newIntent (getActivity (), crime.getId ())

StartActivity (intent)

Return true

Default:

Return super.onOptionsItemSelected (item)

}

}

Notice that the onOptionsItemSelected (MenuItem) method returns a Boolean value. Once the menu item event processing is complete, a true value should be returned to indicate that all tasks have been completed. In addition, in the default case expression, the superclass version method is called if the menu item ID does not exist.

Run the CriminalIntent application, try using the menu, add some crime records and edit them.

At this point, the study on "how Android creates and responds to menu events" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report