Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add menu items to Textview by Android in Java

Shulou Source: shulou.com Published: 2022-06-02 01:09:47 09月20日 Update

Today, I will talk to you about how Android in Java adds menu items to Textview. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Background description:

There is a Textview and a Button on the interface, and when we click Textview, a menu pops up, then we select an option, and when we finally click the button, we execute the corresponding function according to the selected menu item.

Solution.

There are three steps in total:

1. First create the menu folder under the res folder, and then create the menu file main.xml

two。 To add a menu to Activity, and how to add it, first define the menu button PopupMenu, and then override the menu listening event

3. Finally, give the control that needs the pop-up menu (such as adding the property android:onClick= "menuClick" to Textview)

Specific steps: 1. Create a menu file

two。 Add menus to activity and override menu listening events

Create pop-up menu variabl

Private PopupMenu popupMenu;// definition menu button

Override menu monitoring events

/ / rewrite menu listening event public void menuClick (View view) {popupMenu = new PopupMenu (this,view); getMenuInflater () .inflate (R.menu.mainjold popupMenu.getMenu ()) PopupMenu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener () {@ Override public boolean onMenuItemClick (MenuItem item) {switch (item.getItemId ()) {case R.id.to_detail: flag.setText ("detail enhancement"); break Case R.id.to_fudiao: flag.setText ("embossed effect"); break; case R.id.to_mosic: flag.setText ("mosaic"); break Case R.id.to_frost: flag.setText ("dot"); break; case R.id.to_spread: flag.setText ("ground glass"); break;} return true ); popupMenu.show ();} 3. Add menu response properties to the control

What are the collection classes in Java? the sets in Java are mainly divided into four categories: 1, List list: ordered, repeatable; 2, Queue queue: ordered, repeatable; 3, Set collection: non-repeatable; 4, Map mapping: disorder, key unique, value is not unique.

After reading the above, do you have any further understanding of how Android adds menu items to Textview in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Menu event file monitor content button order property control folder step selection three function method variable effect time more ground glass Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information macOS Apple Redmi