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

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

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>
上一篇:Unity UGUI——开源


下一篇:JavaScript基础细讲