码迷,mamicode.com
首页 > 移动开发 > 详细

第二章实例:Android窗口菜单显示

时间:2016-02-02 23:20:39      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享

package test.main.cls;

import com.example.popupwindow.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.PopupWindow;
import android.widget.Toast;

public class PopupWindows extends Activity {

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderIcon(R.drawable.ic_launcher);
        SubMenu submenu = menu.addSubMenu("系统修复");//该方法返回submenu接口对象,想必menu实现了submenu接口
        submenu.add(0, 0, 0, "自动修复").setIcon(R.drawable.ic_launcher);
        submenu.add(0, 1, 0, "修复外存").setIcon(R.drawable.ic_launcher);
        submenu.add(0, 2, 0, "修复硬件").setIcon(R.drawable.ic_launcher);
        //------------------------------------------
        menu.setHeaderIcon(R.drawable.ic_launcher);
        SubMenu submenus = menu.addSubMenu("全局修复");//该方法返回submenu接口对象,想必menu实现了submenu接口
        submenus.add(0, 0, 0, "完全修复").setIcon(R.drawable.ic_launcher);
        submenus.add(0, 1, 0, "无损修复").setIcon(R.drawable.ic_launcher);
        submenus.add(0, 2, 0, "完美修复").setIcon(R.drawable.ic_launcher);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu submenu = menu.addSubMenu("毁灭系统");//该方法返回submenu接口对象,想必menu实现了submenu接口
        submenu.add(0, 0, 0, "全盘毁灭").setIcon(R.drawable.ic_launcher);
        submenu.add(0, 1, 0, "毁灭外存").setIcon(R.drawable.ic_launcher);
        submenu.add(0, 2, 0, "毁灭硬件").setIcon(R.drawable.ic_launcher);
        //submenu.add(0, 4, 0, "毁灭电池").setIcon(R.drawable.ic_launcher).setIntent(/*Intent*/);
        //submenu.add(0, 3, 0, "毁灭屏幕").setShortcut(‘F‘, ‘5‘);
        //submenu.setGroupCheckable(0, true, true);按钮组是否可勾选
        //-----------------------------------------
        SubMenu submenu1 = menu.addSubMenu("发送诈骗信息");
        submenu1.add(0, 0, 0, "发送给全部好友");
        submenu1.add(0, 1, 0, "发送给最近联系");
        submenu1.add(0, 2, 0, "随机发送");
        submenu1.add(0, 3, 0, "发送国外");
        
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onOptionsMenuClosed(Menu menu) {
        // TODO Auto-generated method stub
        super.onOptionsMenuClosed(menu);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentv = this.getLayoutInflater().inflate(R.layout.main_layout, null);
        this.setContentView(contentv);
        //-------------------------------------------
        Button btn = (Button)findViewById(R.id.clickssss);
        final PopupMenu pMenu = new PopupMenu(PopupWindows.this,btn);
        this.getMenuInflater().inflate(R.menu.popup_menu, pMenu.getMenu());
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                pMenu.show();
            }
        });
        pMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(PopupWindows.this, item.getTitle(), Toast.LENGTH_LONG).show();
                return true;
            }
        });
        ///------------------------------------------
        //注意::findViewById需要setContentView后可用
        this.registerForContextMenu(contentv);//给一个View绑定上下文菜单
        
    }
}

xml:
//
main_layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/clickssss" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出"/> </LinearLayout>
xml:
//menu/popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group android:checkableBehavior="none">
        <item android:id="@+id/menu_1"
            android:title="这是一个弹出菜单"/>
        <item android:id="@+id/menu_2"
            android:title="其用法也非常简单"/>
        <item android:id="@+id/menu_3"
            android:title="控件使用不是重点"/>
        <item android:id="@+id/menu_4"
            android:title="重点是要理解代码"/>
             
    </group>

</menu>

 

第二章实例:Android窗口菜单显示

标签:

原文地址:http://www.cnblogs.com/homes/p/5178647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!