Thursday, February 27, 2014

Seeking guidance: how to customize the content under Android menu key on your phone?


Usually when we click the menu button on Android phones, it will pop up a list of menu options, so that we can carry out other operations. In Android program for a View (such as an EditText) is relatively easy to add menu functions, such as:
this.registerForContextMenu (mEditText); then rewrite onCreateContextMenu () method on the line, press the EditText after our custom pop-up menu.
I want to add menu functions to their Android application, that is, to achieve: the phone's menu button is lit, and when I click the menu button on the phone after the pop-up list of my custom menu options. I do not know how?<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
activity code:
 @ Override 
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater (). inflate (R.menu.option, menu) ;/ / option.xml defined
under res / menu / directoryreturn true;
}

@ Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId ()) {
case R.id.menu_settings:
/ / Do something
break; / / later processed here can return true;
}
/ / TODO Auto-generated method stub
return super.onOptionsItemSelected (item);
}


res / menu / option.xml, menus are defined here.
  

android: id = "@ + id / menu_settings"
android: orderInCategory = "100"
android: showAsAction = "never"
android: title = "@ string / menu_settings" />


Reply:
Thank you! Know, the new general engineering Activity in Android will automatically rewrite a onCreateOptionMenu () method, but I have not used it to go, always like what Button to replace it with.
It is necessary to look into the matter, thank you tantahe!
Reply:

No comments:

Post a Comment