Next, let's optimize the calculator in the previous article. Let's look back at the App we have implemented, and what we lack compared to a mature App. The most basic menu is the menu. Let's talk about the menu below.
As we mentioned earlier, we have rewritten two methods in MainActivity. onCreate and onCreateOptionsMenu are the methods for creating menus. Like controls, menus are created using the xml Description Language (layout file) and java code. We can see that the automatically generated onCreateOptionsMenu method has the following sentence: getMenuInflater (). inflate (R. menu. activity_main, menu); load the menu defined in the layout file to the menu instance of the current Activity. Now that the menu is defined in the layout file, open the menu layout file/Calculator/res/menu/activity_main.xml. There should be only the following code:
1 <menu xmlns:android="http://schemas.android.com/apk/res/android">2 <item android:id="@+id/menu_settings"3 android:title="@string/menu_settings"4 android:orderInCategory="100"5 android:showAsAction="never" />6 </menu>
Of course, when we run our program, press the menu key and there should be only one setting item in the menu. After clicking it, there is no response, because we have not added an event listening handler for the menu.
Next, modify the menu and change the setting item to About to display the application information. Then, add an exit menu and add event listening for the menu.
1 <menu xmlns:android="http://schemas.android.com/apk/res/android">2 <item android:id="@+id/menu_about"3 android:title="@string/menu_about"4 android:orderInCategory="100"5 android:showAsAction="never" />6 <item android:id="@+id/menu_exit" android:orderInCategory="101" android:title="@string/menu_exit" android:showAsAction="never"></item>7 </menu>
There are three types of menus: optinosMenu, ContextMenu, and subMenu ), the option menu is the menu that appears after we press the menu key, that is, the menu we are adding above. The option menu is in Activity, therefore, the handler for adding the option menu is very simple. You only need to override the onOptionsItemSelected method in the Activity. As the name suggests, this method is called when a menu is clicked. Its parameters are the instances of the clicked menu items.
1 @ Override 2 public boolean onOptionsItemSelected (MenuItem item) {3 // It seems useless. It can be added or not, but it cannot be guaranteed that google will not extend the code 4 super in the base class in the future. onOptionsItemSelected (item); 5 6 switch (item. getItemId () {7 case R. id. menu_about: 8 this. createAlertDialog (); 9 break; 10 11 case R. id. menu_exit: 12 finish (); 13 break; 14} 15 16 return false; 17} 18 19/** 20 * create About dialog box 21 */22 private void createAlertDialog () {23 Dialog aboutDialog = new AlertDialog. builder (this ). setTitle ("About") 24. setPositiveButton ("OK", new DialogInterface. onClickListener () {25 public void onClick (DialogInterface diich, int which) {26 dialog. cancel (); 27} 28 }). setMessage ("Calculator V1.0 \ r \ nAuthor: Null Pointer \ r \ nInstruction :...... "). create (); 29 aboutDialog. show (); 30}
AlertDialog is used to display the About information. As its name implies, AlertDialog is a pop-up window. Friends who use Android should often see it.
When we click the text box, the system's input method will still pop up, and the user experience is not very good. We will block the input method. It is actually the attribute of EditText. Just add this sentence: inputText. setInputType (InputType. TYPE_NULL );
End.