当前位置: 代码迷 >> Android >> Android的Menu状态动态设立方法onPrepareOptionsMenu(Menu menu)
  详细解决方案

Android的Menu状态动态设立方法onPrepareOptionsMenu(Menu menu)

热度:47   发布时间:2016-05-01 14:04:18.0
Android的Menu状态动态设置方法onPrepareOptionsMenu(Menu menu)

覆盖onPrepareOptionsMenu(Menu menu)这个方法非常的好用,我们可以定义boolean值来进行判断,如果满足条件就可以把菜单栏动态设置成某些状态,比如是否可见,text值等等。

比如我设定一个布尔值addingNew=flase,然后在其他方法体中对addingNew进行重新赋值。在覆盖onPrepareOptionsMenu(Menu menu)中,我们对addingNew进行判断,如果它的值是ture,我们ID为REMOVE_TODO的菜单项就设置为text=“cancel”。否则,我们ID为REMOVE_TODO的菜单项就设置为text=“remove”。

总之,就是可以进行一些判断,然后动态设置我们的菜单项状态。



另外一点,我们应该积累经验,学会很好的运用boolean值,这在很多地方都很有用。



下面是我的程序,从中可以总结出很多经验。

public class ToDolist extends Activity {
??? /** Called when the activity is first created. */
???
??? static final private int ADD_NEW_TODO=Menu.FIRST;
??? static final private int REMOVE_TODO=Menu.FIRST+1;
??? private boolean addingNew=false;//设置这个值来进行控制
??? private ListView myListView;
??? private EditText myEditText;
??? private ArrayList<String> todoItems;
??? private ArrayAdapter<String> aa;
???
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);?
???????
??????? myListView=(ListView)findViewById(R.id.myListView);
??????? myEditText=(EditText)findViewById(R.id.myEditText);
??????? todoItems=new ArrayList<String>();
???????
??????? int resID=R.layout.todolist;
??????? aa=new ArrayAdapter<String>(this, resID, todoItems);
??????? myListView.setAdapter(aa);
???????
??????? myEditText.setOnKeyListener(new OnKeyListener(){

?? @Override
?? public boolean onKey(View v, int keyCode, KeyEvent event) {
??? // TODO Auto-generated method stub
??? if(event.getAction()==KeyEvent.ACTION_DOWN){
???? if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
????? todoItems.add(0, myEditText.getText().toString());
????? aa.notifyDataSetChanged();
????? myEditText.setText("");
????? cancelAdd();//初始设定REMOVE_TODO菜单项不可见,myEditText不可见
????? return true;
???? }
??? }
??? return false;
?? }});
??????? registerForContextMenu(myListView);//设置myListView为一个ContextMenu的接收者
??
??? }
???
??? public boolean onCreateOptionsMenu(Menu menu){
???? super.onCreateOptionsMenu(menu);
???? MenuItem itemAdd=menu.add(0, ADD_NEW_TODO, Menu.NONE, R.string.add_new);
???? MenuItem itemRem=menu.add(0, REMOVE_TODO, Menu.NONE, R.string.remove);
????
???? itemAdd.setIcon(R.drawable.add_new_item);
???? itemRem.setIcon(R.drawable.remove_item);
????
???? itemAdd.setShortcut('0', 'a');
???? itemRem.setShortcut('1', 'r');
????
? return true;
????
??? }
???
???
??? public void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){
???? super.onCreateContextMenu(menu, v, menuInfo);
????
???? menu.setHeaderTitle("Selected To Do Item");
???? menu.add(0, REMOVE_TODO, Menu.NONE, R.string.remove);
????
??? }//创建了ContextMenu
???
??? @Override
??? public boolean onPrepareOptionsMenu(Menu menu){
????
???? super.onPrepareOptionsMenu(menu);
???? int idx=myListView.getSelectedItemPosition();
????
???? //通过addingNew的判断来确定REMOVE_TODO的text的值
???? String removeTitle=getString(addingNew? R.string.cancel:R.string.remove);
????
???? MenuItem removeItem=menu.findItem(REMOVE_TODO);
???? removeItem.setTitle(removeTitle);
????
???? //只有当在添加的状态下(addingNew=true)或者ListView被selected的情况下REMOVE_TODO菜单项才可见
???? removeItem.setVisible(addingNew||idx>-1);
????
? return true;
????
??? }
???
??? @Override
??? public boolean onOptionsItemSelected(MenuItem item){
???? super.onOptionsItemSelected(item);
????
???? int index=myListView.getSelectedItemPosition();
????
???? switch(item.getItemId()){
???? case(REMOVE_TODO):{
????? //此时的REMOVE_TODO菜单的text可能有两种状态:1.cancel 2.remove
????? if(addingNew){
?????? //当addingNew=true的时候,REMOVE_TODO菜单的text=“cancel”,这时候我们就执行相应的取消操作
?????? cancelAdd();
????? }else{
?????? removeItem(index);
?????? //当addingNew=false的时候,REMOVE_TODO菜单的text=“remov”,这时候我们就执行相应的删除操作
????? }
????? return true;
???? }
???? case(ADD_NEW_TODO):{
????? addNewItem();
????? return true;
???? }
???? }
? return false;????
??? }
???
??? @Override
??? public boolean onContextItemSelected(MenuItem item){
???? super.onContextItemSelected(item);
????
???? switch (item.getItemId()){
???? case(REMOVE_TODO):{
????? AdapterView.AdapterContextMenuInfo menuInfo;
????? menuInfo=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
????? int index=menuInfo.position;
?????
????? removeItem(index);
????? return true;
???? }
???? }
? return false;
????
??? }
???
??? //在下面3个方法总执行相应的工作,并且对addingNew进行控制
private void addNewItem() {
? // TODO Auto-generated method stub]
? addingNew=true;
? myEditText.setVisibility(View.VISIBLE);
? myEditText.requestFocus();
?
}

private void removeItem(int _index) {
? // TODO Auto-generated method stub
? todoItems.remove(_index);
? aa.notifyDataSetChanged();
?
}

private void cancelAdd() {
? // TODO Auto-generated method stub
? addingNew=false;
? myEditText.setVisibility(View.GONE);
}
???
}

?

?

:博客涉及的源码请在千寻资源库:www.qxzyk.com 下载获取,谢谢支持。

  相关解决方案