只要有下面两部分就可以初步的实现一个菜单的操作了噢
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.sub_menu, menu);
return true;
}
onCreateOptionsMenu只在选项菜单第一次显示时调用,且只能调用一次
下面是一个菜单及子菜单的示例代码,要仔细阅读哟,看过之后让自己恍然大悟的东西
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class AddressListActivity extends Activity {
private static final int SUBMENU = 10;
private static final int SUBITEM1 = SUBMENU+1;
private static final int SUBITEM2 = SUBMENU+2;
private static final int SUBITEM3 = SUBMENU+3;
private static final int SAVE_AS = SUBMENU+4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.address_list, menu);
SubMenu subMenu = menu.addSubMenu(1,SUBMENU,1,"通讯录");
subMenu.add(20,SUBITEM1,1,"大哥");
subMenu.add(20,SUBITEM2,2,"二哥");
subMenu.add(20,SUBITEM3,3,"三哥");
subMenu.setGroupCheckable(20,true,false);
//可选,true,false单选或多选
/* MenuItem item = subMenu.getItem(1);
item.setCheckable(true);
item.setChecked(true);//处于选种状态
//设置单个选择
*/
//subMenu.findItem(SUBITEM3).setCheckable(true);设置单个选择
注意:findItem与getItem的区别
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.newFile:
Toast.makeText(this,"new file",Toast.LENGTH_LONG).show();
break;
case R.id.internet:
break;
case R.id.exit:
this.finish();
break;
case DELETE:
menu.removeGroup(1);
menu.removeItem(SAVE_AS);
break;
}
return super.onOptionsItemSelected(item);
}
}
下面是利用for循环实现每一项的监听注册:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;
public class MenuTestActivity1 extends Activity implements OnMenuItemClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.optionsmenu,menu);
for(int i=0;i<menu.size();i++){
menu.getItem(i).setOnMenuItemClickListener(this);
}
//实现每一项的监听注册
return true;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.newFile:
Toast.makeText(this,"new file",Toast.LENGTH_LONG).show();
break;
case R.id.exit:
this.finish();
break;
}
return false;
}
}
下面是上下文菜单的调用方法
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MenuTestActivity4 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv= (TextView)this.findViewById(R.id.textView);
this.registerForContextMenu(tv);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.subitem1:
Toast.makeText(this,"subitem1",Toast.LENGTH_LONG).show();
break;
case R.id.subitem2:
//this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
int i=1;
//每次上下文菜单显示都会调用
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.sub_menu,menu);
i++;
menu.getItem(0).setTitle("x"+i);
super.onCreateContextMenu(menu, v, menuInfo);
}
}