Write an article when you are idle. Recently, I am going to take a look at IOS development. It is said that I can develop IOS apps on a computer that supports virtualization, while Object-C is quite different from Java (complicated ). But I have an IOS terminal and want to get something to play. So in addition to continuing to learn about Android, I also want to fix Object-C.
Let's talk a little about it. Next, let's take a look at our Learning content and the Menu design. The Menu is the Menu that we press the Menu key to pop up (it seems a bit useless), but there are many ways to display it, which can be displayed in the current view, you can also switch a view.
What we are talking about today is popped up in the current view. Two options are displayed. One is to change the content of Textview and the other is to exit the program. Okay. Here we are:
Step 1: Create a project Ep. Menu. I will not change the view and activity name.
Step 2: Write activity_main.xml to add an ID to Textview.
[Java]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</RelativeLayout>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</RelativeLayout>
Step 3: Focus on MainActivity. java, which mainly writes menu options and events triggered after selection:
[Java]
Package com. example. ep. menu;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView TV;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Obtain the Menu Control
GetMenuInflater (). inflate (R. menu. main, menu );
// Add options (group ID, option ID, sorting, and title)
Menu. add (0, 0, 0, "set ");
Menu. add (0, 1, "exit ");
Return super. onCreateOptionsMenu (menu );
}
Public boolean onOptionsItemSelected (MenuItem item ){
Super. onOptionsItemSelected (item );
// Obtain the option ID
Switch (item. getItemId ()){
Case 0:
TV = (TextView) findViewById (R. id. tv1 );
TV. setText ("Joven ");
Break;
Case 1:
This. finish ();
Break;
}
Return true;
}
}
Package com. example. ep. menu;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView TV;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Obtain the Menu Control
GetMenuInflater (). inflate (R. menu. main, menu );
// Add options (group ID, option ID, sorting, and title)
Menu. add (0, 0, 0, "set ");
Menu. add (0, 1, "exit ");
Return super. onCreateOptionsMenu (menu );
}
Public boolean onOptionsItemSelected (MenuItem item ){
Super. onOptionsItemSelected (item );
// Obtain the option ID
Switch (item. getItemId ()){
Case 0:
TV = (TextView) findViewById (R. id. tv1 );
TV. setText ("Joven ");
Break;
Case 1:
This. finish ();
Break;
}
Return true;
}
}
Now let's preview the result:
There is one more option here because the menu. xml in our menu folder is set to a value. We only need to delete it.
Let's take a look:
This time OK. Let's click set to try it:
Click exit to see the following information:
Captured many times to capture such a classic image ,--.
Now the menu design in this chapter is complete. Try it.