Android --- Create Menu level 2 sub-Menu

Source: Internet
Author: User

[Do not talk about it, just go to the Code]

[Java]
Package com. bison;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Html;
Import android. view. ContextMenu;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. view. Menu;
Import android. view. SubMenu;
Import android. view. View;
Import android. view. View. OnLongClickListener;
Import android. widget. TextView;
 
Public class TestOneActivity extends Activity {
// Declare TextView
Private TextView TV;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Declare Html code
String strHtml = "<font color = 'red'> This is red </font>" + "<br>"
+ "<Font color = 'green'> This is green </font> ";
 
// Instantiate
TV = new TextView (this );
// Displayed text
TV. setText (Html. fromHtml (strHtml ));
// Set the text size
TV. setTextSize (24 );
// Load TextView to display the page
SetContentView (TV );
// Context menu for TV Registration
RegisterForContextMenu (TV );
 
// The statement can be long-pressed
TV. setLongClickable (true );
TV. setOnLongClickListener (new OnLongClickListener (){
 
Public boolean onLongClick (View v ){
// Display the context menu
V. showContextMenu ();
// Change to true
Return true;
}
});
 
}
 
/** Set the Menu Method */
Private void getMenu (Menu menu, String str ){
Menu. add (1, 1, 1, str + "1 ");
Menu. add (1, 2, 2, str + "2 ");
Menu. add (1, 3, 3, str + "3 ");
// Declare sub-menu
SubMenu mSubMenu1 = menu. addSubMenu (str + "file ");
MSubMenu1.add (10, 5, 4, str + "new ");
MSubMenu1.add (10, 6, 6, str + "open ");
SubMenu mSubMenu2 = menu. addSubMenu (str + "edit ");
MSubMenu2.add (20, 7, 8, str + "copy ");
MSubMenu2.add (20, 8, 9, str + "delete ");
}
 
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
GetMenu (menu, "context ");
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenu (menu, "Menu button ");
Return super. onCreateOptionsMenu (menu );
}
 
}
Package com. bison;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Html;
Import android. view. ContextMenu;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. view. Menu;
Import android. view. SubMenu;
Import android. view. View;
Import android. view. View. OnLongClickListener;
Import android. widget. TextView;

Public class TestOneActivity extends Activity {
// Declare TextView
Private TextView TV;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Declare Html code
String strHtml = "<font color = 'red'> This is red </font>" + "<br>"
+ "<Font color = 'green'> This is green </font> ";

// Instantiate
TV = new TextView (this );
// The displayed text is www.2cto.com.
TV. setText (Html. fromHtml (strHtml ));
// Set the text size
TV. setTextSize (24 );
// Load TextView to display the page
SetContentView (TV );
// Context menu for TV Registration
RegisterForContextMenu (TV );

// The statement can be long-pressed
TV. setLongClickable (true );
TV. setOnLongClickListener (new OnLongClickListener (){

Public boolean onLongClick (View v ){
// Display the context menu
V. showContextMenu ();
// Change to true
Return true;
}
});

}

/** Set the Menu Method */
Private void getMenu (Menu menu, String str ){
Menu. add (1, 1, 1, str + "1 ");
Menu. add (1, 2, 2, str + "2 ");
Menu. add (1, 3, 3, str + "3 ");
// Declare sub-menu
SubMenu mSubMenu1 = menu. addSubMenu (str + "file ");
MSubMenu1.add (10, 5, 4, str + "new ");
MSubMenu1.add (10, 6, 6, str + "open ");
SubMenu mSubMenu2 = menu. addSubMenu (str + "edit ");
MSubMenu2.add (20, 7, 8, str + "copy ");
MSubMenu2.add (20, 8, 9, str + "delete ");
}

@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
GetMenu (menu, "context ");
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenu (menu, "Menu button ");
Return super. onCreateOptionsMenu (menu );
}

}


PS: note that the context menu must be registered and showContextMenu ()


 

From today and now... Bi... Column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.