标签:android style c class blog code
安卓菜单有三种菜单。
选项菜单: 点击系统菜单按钮会触发
上下文菜单:长按屏幕触发
子菜单:某一个菜单的下一级菜单
具体的描叙:http://blog.csdn.net/zqiang_55/article/details/7038085
写的简洁易懂。呵呵,其他的都太复杂了
我参考这个思路 写了一个演示代码。
package cn.wuwenfu.menudemo; import com.example.menudemo.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button contextMenuButton;//上下文菜单 private Button subMenuButton;//子菜单 private Button optionMenuButton;//选项菜单 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mainactivity); //获得3个按钮的控件 subMenuButton = (Button)this.findViewById(R.id.submenu_button); contextMenuButton = (Button)this.findViewById(R.id.contextmenu_button); optionMenuButton = (Button)this.findViewById(R.id.optionmenu_button); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); contextMenuButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent(); in.setClass(MainActivity.this, ContextMenuActivity.class); MainActivity.this.startActivity(in); } }); subMenuButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent(); in.setClass(MainActivity.this, SubMenuActivity.class); MainActivity.this.startActivity(in); } }); } //选项菜单 //如果触屏的平板则要注意sdk版本,过高会导致看不到菜单的图标 //两种方法:第一种就是代码生成。第二种就是xml文件。 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub //add方法参数说明:参数一是分组的id,参数二:菜单的ID(最重要);参数三:菜单的排序,默认值0表示按照添加的顺序显示 /*menu.add(0, 0, 0, "查看"); menu.add(0, 1, 0, "修改"); menu.add(0, 2, 0, "删除"); menu.add(1, 3, 0, "查看"); menu.add(1, 4, 0, "修改"); menu.add(1, 5, 0, "删除"); //隐藏分组0 menu.setGroupVisible(0, false); */ //第二种方式 MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.option_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* //是代码生成的,则是具体的数字 case 0: Toast.makeText(MainActivity.this, "你点击率查看", Toast.LENGTH_LONG).show(); return true; case 1: Toast.makeText(MainActivity.this, "你点击率修改", Toast.LENGTH_LONG).show(); return true; case 2: Toast.makeText(MainActivity.this, "你点击率删除", Toast.LENGTH_LONG).show(); return true; case 3: Toast.makeText(MainActivity.this, "你点击率查看1", Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(MainActivity.this, "你点击率修改1", Toast.LENGTH_LONG).show(); return true; case 5: Toast.makeText(MainActivity.this, "你点击率删除1", Toast.LENGTH_LONG).show(); return true; */ //上面的是基于add的菜单。下面的则是基于xml菜单 //由于这里采用的xml生成,id则必须是android:id对应的。 case R.id.menu1: Toast.makeText(MainActivity.this, "菜单1", Toast.LENGTH_LONG).show(); return true; case R.id.menu2: Toast.makeText(MainActivity.this, "菜单2", Toast.LENGTH_LONG).show(); return true; case R.id.menu3: Toast.makeText(MainActivity.this, "菜单3", Toast.LENGTH_LONG).show(); return true; } return false; } }
package cn.wuwenfu.menudemo; import com.example.menudemo.R; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class SubMenuActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.submenuactivity); } @Override public boolean onCreateOptionsMenu(Menu menu) { int base = Menu.FIRST; // 一个menu可以包括多个子菜单 SubMenu subMenu = menu.addSubMenu(0, base+1, Menu.NONE, "系统设置"); // 子菜单可以包括多个菜单项 MenuItem menuitem1 = subMenu.add(base, base+1, base+1, "显示设置"); subMenu.add(base, base+2, base+2, "网络设置"); subMenu.add(base, base+3, base+3, "高级设置"); subMenu.add(base, base+4, base+4, "安全设置"); // 显示菜单请返回true return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub System.out.println("----->"+item.getGroupId()); //这里要先判断组id 再继续判断子菜单 switch (item.getGroupId()) { case 1: switch (item.getItemId()) { case 2: Toast.makeText(SubMenuActivity.this, "显示", Toast.LENGTH_LONG).show(); return true; case 3: Toast.makeText(SubMenuActivity.this, "网络", Toast.LENGTH_LONG).show(); return true; case 4: Toast.makeText(SubMenuActivity.this, "高级", Toast.LENGTH_LONG).show(); return true; case 5: Toast.makeText(SubMenuActivity.this, "安全", Toast.LENGTH_LONG).show(); return true; } return false; default: break; } return false; } }
package cn.wuwenfu.menudemo; /* * 2014-05-23 16:08 * 实现的效果:一个listview。 * 上下文菜单: 删除 修改 查看 * */ import java.util.ArrayList; import java.util.HashMap; import com.example.menudemo.R; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class ContextMenuActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.contextmenuactivity); // 获得控件 ListView list = (ListView) findViewById(R.id.android_list1234); // 数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < 5; i++) { HashMap<String, Object> m = new HashMap<String, Object>(); m.put("name", "吴文付" + i);//添加不同数据。 m.put("sex", "男"); m.put("age", "20"); listItem.add(m); } // 适配器 SimpleAdapter listItemAdapter = new SimpleAdapter( // activity this, // 数据 listItem, // 样式 R.layout.itemlist, // 名称与样式的对应 new String[] { "name", "sex", "age" }, new int[] { R.id.name, R.id.sex, R.id.age }); // 将适配器放入控件中,显示 list.setAdapter(listItemAdapter); // 注册上下文菜单 // 第一种:通过监听事件 /* * list.setOnCreateContextMenuListener( new * OnCreateContextMenuListener() { * * @Override public void onCreateContextMenu(ContextMenu menu, View * arg1, ContextMenuInfo arg2) { // TODO Auto-generated method stub * menu.setHeaderTitle("上下文"); menu.add(0, 0, 0, "查看"); menu.add(0, 1, * 0, "修改"); menu.add(0, 2, 0, "删除"); * * } }); */ // 第二种注册方法 registerForContextMenu(list); } // 上下文菜单,本例会通过长按条目激活上下文菜单 // 第二种注册方法需要它 @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { menu.setHeaderTitle("上下文菜单"); // 添加菜单项 menu.add(0, 0, 0, "查看"); menu.add(0, 1, 0, "修改"); menu.add(0, 2, 0, "删除"); } // 上下文菜单 // 菜单单击响应() @Override public boolean onContextItemSelected(MenuItem item) { // 获取当前被选择的菜单项的信息 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); Log.i("braincol", String.valueOf(info.id)); System.out.println("-------->" + String.valueOf(info.id)); // 获得选中的视图对象 TextView tv = (TextView) info.targetView.findViewById(R.id.name); switch (item.getItemId()) { case 0: // 在这里添加处理代码 Toast.makeText(ContextMenuActivity.this, "查看" + tv.getText(), Toast.LENGTH_LONG).show(); break; case 1: // 在这里添加处理代码 Toast.makeText(ContextMenuActivity.this, "修改" + tv.getText(), Toast.LENGTH_LONG).show(); break; case 2: // 在这里添加处理代码 Toast.makeText(ContextMenuActivity.this, "删除" + tv.getText(), Toast.LENGTH_LONG).show(); break; } return true; } }
工程截图:
运行效果截图:
工程地址:
http://download.csdn.net/detail/douniwan123654/7406031
标签:android style c class blog code
原文地址:http://www.cnblogs.com/jsRunner/p/3754410.html