26.Android之Menu菜单活动简单学习

我们经常在手机界面看到菜单Menu活动痕迹,今天学习下。

修改下工程下menu_main.xml文件:

 1 <menu xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
 3     <item android:id="@+id/action_settings" android:title="@string/action_settings"
 4         android:orderInCategory="100" android:showAsAction="never" />
 5 
 6     <item
 7         android:id="@+id/add_item"
 8         android:title="Add"/>
 9 
10     <item
11         android:id="@+id/remove_item"
12         android:title="Remove"/>
13 </menu>

在主Activity_main.xml增加一个按钮,主要实现点击这个按钮就会销毁菜单活动:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
 4     android:paddingRight="@dimen/activity_horizontal_margin"
 5     android:paddingTop="@dimen/activity_vertical_margin"
 6     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 7 
 8 
 9     <Button
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:text="手动销毁一个活动"
13         android:id="@+id/button_dest"
14         android:layout_alignParentTop="true"
15         android:layout_alignParentLeft="true"
16         android:layout_alignParentStart="true" />
17 </RelativeLayout>

最后修改下MainActivity文件:

 1 package com.example.administrator.menudemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends Activity {
12 
13     private Button btn_destroy;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19 
20         btn_destroy = (Button)findViewById(R.id.button_dest);
21         btn_destroy.setOnClickListener(new View.OnClickListener() {
22             @Override
23             public void onClick(View v) {
24                 finish();
25             }
26         });
27     }
28 
29     @Override
30     public boolean onCreateOptionsMenu(Menu menu) {
31         // Inflate the menu; this adds items to the action bar if it is present.
32         getMenuInflater().inflate(R.menu.menu_main, menu);
33         return true;
34     }
35 
36     @Override
37     public boolean onOptionsItemSelected(MenuItem item) {
38         // Handle action bar item clicks here. The action bar will
39         // automatically handle clicks on the Home/Up button, so long
40         // as you specify a parent activity in AndroidManifest.xml.
41         int id = item.getItemId();
42 
43         //noinspection SimplifiableIfStatement
44         if (id == R.id.action_settings) {
45             return true;
46         }
47 
48         if (id == R.id.add_item){
49             Toast.makeText(this,"clicked add item",Toast.LENGTH_LONG).show();
50         }
51 
52         if (id == R.id.remove_item){
53             Toast.makeText(this,"clicked remove item",Toast.LENGTH_LONG).show();
54         }
55 
56         return super.onOptionsItemSelected(item);
57     }
58 }

运行效果

1)按下Menu键(菜单默认不会显示,要按下menu键菜单才会在底部显示)

点击Add选项显示如图:

如果显示在底部菜单不想按返回键退出,可以通过点击"手动销毁一个活动"这个按钮来销毁活动,主要是Activity类提供了一个finish()方法,我们在活动中调用一下这个方法就可以销毁当前活动了。

原文地址:https://www.cnblogs.com/benchao/p/5117579.html