android--菜单

 

android中菜单分为三种,分别是,options menu 选项菜单,sub menu 子菜单,context menu 上下文菜单。下面分别举例演示这三种菜单:


新建一个android project,

options menu,第一种方法,通过xml定义选项菜单,在res文件夹中新建一个名为menu的文件夹,在其内新建一个options_menu.xml的文件:

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <item  
  5.         android:id="@+id/newFile"  
  6.         android:icon="@drawable/ic_launcher"  
  7.         android:title="@string/newFile"  
  8.         />  
  9.     <!-- android:icon 设置菜单选项的图标  
  10.     android:title 设置菜单选项的标题 -->  
  11.     <item   
  12.         android:id="@+id/exit"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:title="@string/exit"  
  15.         />  
  16.   
  17. </menu>  
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/newFile" android:icon="@drawable/ic_launcher" android:title="@string/newFile" /> <!-- android:icon 设置菜单选项的图标 android:title 设置菜单选项的标题 --> <item android:id="@+id/exit" android:icon="@drawable/ic_launcher" android:title="@string/exit" /> </menu>
在MenuTestActivity1中:

view plain copy to clipboard print ?
  1. package cn.sword.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuInflater;  
  7. import android.view.MenuItem;  
  8. import android.view.MenuItem.OnMenuItemClickListener;  
  9. import android.widget.Toast;  
  10.   
  11. public class MenuTestActivity1 extends Activity implements OnMenuItemClickListener{  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean onCreateOptionsMenu(Menu menu) {  
  22.         MenuInflater inflater = this.getMenuInflater();  
  23.         inflater.inflate(R.menu.options_menu, menu);  
  24.         //第一种响应菜单点击事件的方法  
  25.         //为每一个菜单选项添加监听事件  
  26.         //在覆盖的onMenuItemClick()方法中判断并触发监听事件  
  27.         for(int i=0;i<menu.size();i++){  
  28.             menu.getItem(i).setOnMenuItemClickListener((OnMenuItemClickListener) this);  
  29.         }  
  30.   
  31.         return super.onCreateOptionsMenu(menu);  
  32.     }  
  33.   
  34.     /*第二种响应菜单点击事件的方法 覆盖onOptionsItemSelected()方法  
  35.     @Override  
  36.     public boolean onOptionsItemSelected(MenuItem item) {  
  37.         switch(item.getItemId()){  
  38.             case R.id.newFile:  
  39.                 Toast.makeText(this,"new File", Toast.LENGTH_LONG).show();  
  40.                 break;  
  41.             case R.id.exit:  
  42.                 this.finish();//退出程序  
  43.                 break;  
  44.         }  
  45.           
  46.         return super.onOptionsItemSelected(item);  
  47.     }*/  
  48.       
  49.     public boolean onMenuItemClick(MenuItem item){  
  50.         switch(item.getItemId()){  
  51.         case R.id.newFile:  
  52.             Toast.makeText(this, "new File", Toast.LENGTH_LONG).show();  
  53.             break;  
  54.         case R.id.exit:  
  55.             this.finish();  
  56.         }  
  57.           
  58.         return false;  
  59.     }  
  60.       
  61. }  
package cn.sword.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); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = this.getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); //第一种响应菜单点击事件的方法 //为每一个菜单选项添加监听事件 //在覆盖的onMenuItemClick()方法中判断并触发监听事件 for(int i=0;i<menu.size();i++){ menu.getItem(i).setOnMenuItemClickListener((OnMenuItemClickListener) this); } return super.onCreateOptionsMenu(menu); } /*第二种响应菜单点击事件的方法 覆盖onOptionsItemSelected()方法 @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.exit: this.finish();//退出程序 break; } return super.onOptionsItemSelected(item); }*/ 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(); } return false; } }


在模拟器中点击Menu键效果:

android--菜单_第1张图片



options menu,第二种方法,动态添加删除菜单


在MenuTestActivity2中:

view plain copy to clipboard print ?
  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuInflater;  
  7. import android.view.MenuItem;  
  8. import android.widget.Toast;  
  9.   
  10. public class MenuTestActivity2 extends Activity {  
  11. <span style="white-space:pre">    </span>//定义菜单项  
  12.     private static final int SAVE=1;  
  13.     private static final int SAVESAVE_AS = SAVE+1;  
  14.     private static final int DELETE = SAVE+2;  
  15.     //声明一个menu对象  
  16.     Menu menu = null;  
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.widget.Toast; public class MenuTestActivity2 extends Activity { <span style="white-space:pre"> </span>//定义菜单项 private static final int SAVE=1; private static final int SAVE_AS = SAVE+1; private static final int DELETE = SAVE+2; //声明一个menu对象 Menu menu = null;
view plain copy to clipboard print ?
  1.     public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.main);  
  4.     }  
  5.   
  6.     public boolean onCreateOptionsMenu(Menu menu) {  
  7.         MenuInflater inflater = this.getMenuInflater();  
  8.         inflater.inflate(R.menu.options_menu, menu);  
  9.           
  10.         //向菜单中添加菜单项  
  11.         menu.add(1, SAVE, 1, "保存");  
  12.         menu.add(1,SAVE_AS, 2, "另存为");  
  13.         menu.add(1,DELETE, 3, "删除保存");  
  14.         //设置菜单项的图标  
  15.         menu.findItem(SAVE).setIcon(R.drawable.ic_launcher);      
  16.           
  17.         this.menu = menu;  
  18.   
  19.         return true;  
  20.     }  
  21.       
  22. <span style="white-space:pre">    </span>//动态的修改菜单title   
  23.     int i=1;  
  24.     public boolean onPrepareOptionsMenu(Menu menu) {  
  25.         MenuItem item = menu.findItem(SAVE_AS);  
  26.         item.setTitle(item.getTitle() + "" + i);  
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.options_menu, menu); //向菜单中添加菜单项 menu.add(1, SAVE, 1, "保存"); menu.add(1,SAVE_AS, 2, "另存为"); menu.add(1,DELETE, 3, "删除保存"); //设置菜单项的图标 menu.findItem(SAVE).setIcon(R.drawable.ic_launcher); this.menu = menu; return true; } <span style="white-space:pre"> </span>//动态的修改菜单title int i=1; public boolean onPrepareOptionsMenu(Menu menu) { MenuItem item = menu.findItem(SAVE_AS); item.setTitle(item.getTitle() + "" + i);
view plain copy to clipboard print ?
  1.     i++;  
  2.     return super.onPrepareOptionsMenu(menu);  
  3. }  
  4. lt;span style="white-space:pre"> </span>//响应菜单单击事件  
  5. public boolean onOptionsItemSelected(MenuItem item) {  
  6.     switch (item.getItemId()) {  
  7.     case R.id.newFile:  
i++; return super.onPrepareOptionsMenu(menu); } <span style="white-space:pre"> </span>//响应菜单单击事件 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.newFile:
view plain copy to clipboard print ?
  1.     Toast.makeText(this, "new file", Toast.LENGTH_LONG).show();//利用Toast弹出文本信息  
  2.     break;  
  3. case R.id.exit:  
  4.     // Toast.makeText(this, "exit", Toast.LENGTH_LONG).show();  
  5.     this.finish();//关闭Activity  
  6.     break;  
  7. case DELETE:<pre name="code" class="html"><span style="white-space:pre">            </span>//menu.removeGroup(1);//删除一组菜单  
Toast.makeText(this, "new file", Toast.LENGTH_LONG).show();//利用Toast弹出文本信息 break; case R.id.exit: // Toast.makeText(this, "exit", Toast.LENGTH_LONG).show(); this.finish();//关闭Activity break; case DELETE:<pre name="code" class="html"><span style="white-space:pre"> </span>//menu.removeGroup(1);//删除一组菜单 menu.removeItem(SAVE_AS);//删除一个菜单break;}return true;}}

 
 
sub menu 子菜单

利用xml定义菜单参考options menu。

动态的添加子菜单:

在MenuTestActivity3中:

view plain copy to clipboard print ?
  1. package cn.sword.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuInflater;  
  7. import android.view.MenuItem;  
  8. import android.view.SubMenu;  
  9. import android.widget.Toast;  
  10.   
  11. public class MenuTestActivity3 extends Activity {  
  12. <span style="white-space:pre">    </span>//定义菜单项  
  13.     private static final int SUBMENU = 10;  
  14.     private static final int SUBITEM1 = SUBMENU+1;  
  15.     private static final int SUBITEM2 = SUBMENU+2;  
  16.     private static final int SUBITEM3 = SUBMENU+3;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         this.setContentView(R.layout.main);  
  22.     }  
  23.       
  24.     public boolean onCreateOptionsMenu(Menu menu){  
  25.         MenuInflater inflater = this.getMenuInflater();  
  26.         inflater.inflate(R.menu.sub_menu, menu);  
  27.         //添加子菜单  
  28.         SubMenu subMenu = menu.addSubMenu(1,SUBMENU,1,"子菜单");  
  29.         subMenu.add(20,SUBITEM1,1,"subitem1");  
  30.         subMenu.add(20,SUBITEM2,2,"subitem2");  
  31.         subMenu.add(30,SUBITEM3,3,"subitem3");  
  32.           
  33.         subMenu.setGroupCheckable(20, true, false);  
  34.         //第二个参数是否可选  
  35.         //当第三个参数为true时为这组菜单为互斥(单选)菜单  
  36.         //当第三个参数为false时为组菜单为复选菜单  
  37.           
  38.         MenuItem item = subMenu.getItem(0);  
  39.         //item.setCheckable(false);//设置该项菜单为单选或复选  
  40.         item.setChecked(true);//设置菜单的默认选项  
  41.   
  42.         //subMenu.getItem(0).setCheckable(true);  
  43.           
  44.         return true;  
  45.     }  
  46.       
  47.     @Override  
  48.     public boolean onOptionsItemSelected(MenuItem item) {  
  49.         switch(item.getItemId()){  
  50.             case R.id.menuitem1:  
  51.                 Toast.makeText(this,"menuitem1", Toast.LENGTH_LONG).show();  
  52.                 break;  
  53.             case R.id.menuitem2:  
  54.                 Toast.makeText(this,"menuitem2", Toast.LENGTH_LONG).show();  
  55.                 break;  
  56.             case R.id.subitem1:  
  57.                 Toast.makeText(this,"subitem1", Toast.LENGTH_LONG).show();  
  58.                 break;  
  59.             case R.id.subitem2:  
  60.                 Toast.makeText(this,"subitem2", Toast.LENGTH_LONG).show();  
  61.                 break;  
  62.             case SUBITEM1:  
  63.                 Toast.makeText(this,"SUBITEM1", Toast.LENGTH_LONG).show();  
  64.                 break;  
  65.             case SUBITEM2:  
  66.                 Toast.makeText(this,"SUBITEM2", Toast.LENGTH_LONG).show();  
  67.                 //this.finish();//退出程序  
  68.                 break;  
  69.         }  
  70.           
  71.         return super.onOptionsItemSelected(item);  
  72.     }  
  73. }  
package cn.sword.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 MenuTestActivity3 extends Activity { <span style="white-space:pre"> </span>//定义菜单项 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = this.getMenuInflater(); inflater.inflate(R.menu.sub_menu, menu); //添加子菜单 SubMenu subMenu = menu.addSubMenu(1,SUBMENU,1,"子菜单"); subMenu.add(20,SUBITEM1,1,"subitem1"); subMenu.add(20,SUBITEM2,2,"subitem2"); subMenu.add(30,SUBITEM3,3,"subitem3"); subMenu.setGroupCheckable(20, true, false); //第二个参数是否可选 //当第三个参数为true时为这组菜单为互斥(单选)菜单 //当第三个参数为false时为组菜单为复选菜单 MenuItem item = subMenu.getItem(0); //item.setCheckable(false);//设置该项菜单为单选或复选 item.setChecked(true);//设置菜单的默认选项 //subMenu.getItem(0).setCheckable(true); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.menuitem1: Toast.makeText(this,"menuitem1", Toast.LENGTH_LONG).show(); break; case R.id.menuitem2: Toast.makeText(this,"menuitem2", Toast.LENGTH_LONG).show(); break; case R.id.subitem1: Toast.makeText(this,"subitem1", Toast.LENGTH_LONG).show(); break; case R.id.subitem2: Toast.makeText(this,"subitem2", Toast.LENGTH_LONG).show(); break; case SUBITEM1: Toast.makeText(this,"SUBITEM1", Toast.LENGTH_LONG).show(); break; case SUBITEM2: Toast.makeText(this,"SUBITEM2", Toast.LENGTH_LONG).show(); //this.finish();//退出程序 break; } return super.onOptionsItemSelected(item); } }
在模拟器中点击Menu键:

android--菜单_第2张图片

单击子菜单:

android--菜单_第3张图片


context menu 上下文菜单:context menu需要绑定在TextView上并在其上按2s后显示。

在MenuTestActivity4中:

view plain copy to clipboard print ?
  1. package cn.sword.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.ContextMenu;  
  6. import android.view.ContextMenu.ContextMenuInfo;  
  7. import android.view.MenuInflater;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MenuTestActivity4 extends Activity {  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         this.setContentView(R.layout.main);  
  18.         //将上下文菜单绑定到TextView上  
  19.         TextView tv = (TextView) this.findViewById(R.id.textView);  
  20.         this.registerForContextMenu(tv);  
  21.     }  
  22.   
  23.     @Override  
  24.     public boolean onContextItemSelected(MenuItem item) {  
  25.         switch(item.getItemId()){  
  26.             case R.id.subitem1:  
  27.                 Toast.makeText(this, "subitem1",Toast.LENGTH_LONG).show();  
  28.                 break;  
  29.             case R.id.subitem2:  
  30.                 Toast.makeText(this, "subitem2",Toast.LENGTH_LONG).show();  
  31.                 break;  
  32.         }  
  33.         return super.onContextItemSelected(item);  
  34.     }  
  35.   
  36.     //每次上下文菜单显示都会调用  
  37.     public void onCreateContextMenu(ContextMenu menu, View v,  
  38.             ContextMenuInfo menuInfo) {  
  39.           
  40.         MenuInflater inflater = this.getMenuInflater();  
  41.         inflater.inflate(R.menu.sub_menu, menu);  
  42.         super.onCreateContextMenu(menu, v, menuInfo);  
  43.     }  
  44. }  
package cn.sword.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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); //将上下文菜单绑定到TextView上 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: Toast.makeText(this, "subitem2",Toast.LENGTH_LONG).show(); break; } return super.onContextItemSelected(item); } //每次上下文菜单显示都会调用 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { MenuInflater inflater = this.getMenuInflater(); inflater.inflate(R.menu.sub_menu, menu); super.onCreateContextMenu(menu, v, menuInfo); } }
运行模拟器,点击文本内容2s后:

android--菜单_第4张图片

你可能感兴趣的:(android,File,delete,Class,menu,encoding)