关于OptionMenu实例
点击qc1按钮后会弹出图:
点击qc1按钮后会弹出图:
activity的代码:
package com.tmacsky; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MenuTest extends Activity{ protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.gravery); } //通过调用xml布局文件来创建显示menu public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflaterr = getMenuInflater(); inflaterr.inflate(R.menu.option_menu, menu); return true; } //点击了menu上的item按钮后会执行下面的方法函数 public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Toast.makeText(this, item.getTitle(), 2000).show(); return super.onOptionsItemSelected(item); } //菜单关闭之后会执行下面的函数 public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(this, "菜单关闭了", 3000).show(); } //点击menu后先显示下面的文字,即在你点击菜单后,菜单显示之前调用下面的函数 public boolean onPrepareOptionsMenu(Menu menu){ Toast.makeText(this, "在菜单显示之前会调用此操作", 3000).show(); return true; } }
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:title="qa1" android:icon="@drawable/qa"/> <item android:title="qb1" android:icon="@drawable/qc"> <menu> <group android:checkableBehavior="single"> <item android:title="table1"/> <item android:title="table2" /> <item android:title="table3" /> <item android:title="table4" /> </group> </menu> </item> <item android:title="qc1" android:icon="@drawable/qc"> <menu> <item android:checkable="true" android:title="call" android:icon="@drawable/qc"/> <item android:title="Phone" android:icon="@drawable/qc"/> <item android:title="Mess" android:icon="@drawable/qc"/> </menu> </item> </menu>
长按了test中的任何一个list都会弹出下图:
然后你再点上图的qb1,就会弹出:
activity代码:
package com.tmacsky; import java.util.ArrayList; import android.R.anim; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.text.AndroidCharacter; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Toast; public class MenuTest extends ListActivity{ private String[] itemStrings = new String[]{"test1","test2","test3","test4","test5"}; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //设置adapter并绑定 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,itemStrings); setListAdapter(adapter); //注册contextMenu registerForContextMenu(getListView()); } //点击后会提示点击了那个文件 public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub Toast.makeText(this, item.getTitle(), 2000).show(); return true; } //设置长按list弹出后的窗口的title public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub menu.setHeaderTitle("contextMenu"); menu.setHeaderIcon(R.drawable.qa); super.onCreateContextMenu(menu, v, menuInfo); super.getMenuInflater().inflate(R.menu.option_menu, menu); } //通过调用xml布局文件来创建显示menu public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflaterr = getMenuInflater(); inflaterr.inflate(R.menu.option_menu, menu); return true; } //点击了menu上的item按钮后会执行下面的方法函数 public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Toast.makeText(this, item.getTitle(), 2000).show(); return super.onOptionsItemSelected(item); } //菜单关闭之后会执行下面的函数 public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(this, "菜单关闭了", 3000).show(); } //点击menu后先显示下面的文字,即在你点击菜单后,菜单显示之前调用下面的函数 public boolean onPrepareOptionsMenu(Menu menu){ Toast.makeText(this, "在菜单显示之前会调用此操作", 3000).show(); return true; } }