选项菜单和上下文菜单

package com.example.wang.testapp2;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TestActivity3 extends Activity {

    Button bt_1;
    Button bt_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test3);

        bt_1=(Button)findViewById(R.id.bt_1);
        bt_2=(Button)findViewById(R.id.bt_2);

        //给组件添加上下文菜单  Activity是OnCreateContextMenuListener的实现类
        bt_2.setOnCreateContextMenuListener(
                this
        );
        //添加长按监听器
//        bt_2.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//
//                Toast.makeText(TestActivity3.this, "按钮被长按了", Toast.LENGTH_SHORT).show();
//
//                return true;//为true时后面不会再被处理
//            }
//        });

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(TestActivity3.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
            }
        });
        bt_1.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                Toast.makeText(TestActivity3.this, "按钮被常按了", Toast.LENGTH_SHORT).show();

                //事件是否已经被消费,如果下列返回false,程序会继续执行点击事件
                return true;
            }
        });


    }

    //给Activity 添加选项菜单
    //重写一个回调方法

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        //加载菜单文件方式1
        //添加菜单,纯编码方式
//        menu.add(1,1,0,"添加");
//        menu.add(1,2,2,"修改");
//        menu.add(1,3,1,"删除");

        //加载菜单文件方式2
        //1-获得菜单加载器
        MenuInflater m=getMenuInflater();

        //2-加载菜单文件 m.inflate(菜单文件 ,当前菜单);
        m.inflate(R.menu.test3_menu,menu);


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        Toast.makeText(TestActivity3.this, "菜单项的id="+
                item.getItemId()+"菜单项的title="+item.getTitle(), Toast.LENGTH_SHORT).show();

        //怎么判断菜单文件里的菜单哪个被选中了
        if(item.getItemId()==R.id.me_1)
        {
            Toast.makeText(TestActivity3.this, "执行删除动作", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(TestActivity3.this, "执行修改动作", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    //重写创建上下文菜单的方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        //java
        menu.add(1, 0, 1, "字体大小");
        menu.add(1, 1, 2, "背景颜色");

        //添加子菜单
        SubMenu sm=menu.addSubMenu(1, 2, 3, "子菜单");
        sm.add(2,21,0,"子菜单1");
        sm.add(2,22,1,"子菜单2");


        //加载文件
        MenuInflater m=getMenuInflater();
        m.inflate(R.menu.test3_menu,menu);
    }

    //上下文菜单被点击
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(TestActivity3.this, "上下文菜单的标题"+item.getTitle(), Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);
    }
}
TestActivity3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.wang.testapp2.TestActivity3"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拨打电话"
        android:id="@+id/bt_1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上下文菜单按钮"
        android:id="@+id/bt_2"/>

</LinearLayout>
activity_test3
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/me_1" android:title="删除"/>
    <item android:id="@+id/me_2" android:title="修改"/>

</menu>
test3_menu

posted @ 2016-05-09 23:04  1011042043  阅读(278)  评论(0编辑  收藏  举报