Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Menu programming in Android

Shulou Source: shulou.com Published: 2022-05-31 21:46:29 10月25日 Update

This article focuses on "how to implement Menu programming in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement Menu programming in Android".

Option Menu:

Import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class HelloActivity extends Activity {@ Override public boolean onCreateOptionsMenu (Menu menu) {/ / TODO Auto-generated method stub super.onCreateOptionsMenu (menu); menu.add (0, Menu.FIRST+1, 1, R.string.menu_open); menu.add (0, Menu.FIRST+2, 2, R.string.menu_edit) Menu.add (0, Menu.FIRST+3, 3, R.string.menu_update); menu.add (0, Menu.FIRST+4, 4, R.string.menu_close); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {/ / TODO Auto-generated method stub super.onOptionsItemSelected (item); switch (item.getItemId ()) {case Menu.FIRST+ 1: {this.setTitle ("Open Text!"); break } case Menu.FIRST + 2: {this.setTitle ("Edit Text!"); break;} case Menu.FIRST + 3: {this.setTitle ("Update Text!"); break;} case Menu.FIRST + 4: {this.setTitle ("Close Text!"); break;}} return true;} @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); this.setContentView (R.layout.main);}

Only need to rewrite the two functions of the parent class Activity, which is very convenient. In other words, Android has already written the relevant code for you, all you have to do is fill in some menu items and write specific operation code for the ItemId of each menu item.

At this point, I believe you have a deeper understanding of "how to achieve Menu programming in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Programming code content menus learning practical deeper two that is interests functions practicality practicality simplicity methods more friends websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Huawei vpn MySQL