长按显示菜单选项,系统菜单,上下文菜单
今天看视频学了新的知识,那就是通过两种不同的方式实现菜单(java代码实现、xml文件实现)菜单。先来上几个程序运行截图:
这是三个小案例的截图,我就不废话那么多,直接上例子代码了。
案例一代码就是一个java代码。是通过java代码实现菜单的。
MainActivity.java
package cn.edu.aynu.rjxy.activity; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { //getMenuInflater().inflate(R.menu.main, menu); //使用java代码实现菜单,最多显示6个子菜单 MenuItem menuItem = menu.add(1001, 100, 1, "系统菜单"); menuItem.setTitle("菜单一"); MenuItem menuItem2 = menu.add(1001, 101, 2, "用户菜单"); menuItem2.setTitle("菜单二"); MenuItem menuItem3 = menu.add(1001, 102, 2, "客户菜单"); menuItem3.setTitle("菜单三"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 100: Toast.makeText(MainActivity.this, "选择了菜单一", 0).show(); break; case 101: Toast.makeText(MainActivity.this, "选择了菜单二", 0).show(); break; case 102: Toast.makeText(MainActivity.this, "选择了菜单三", 0).show(); break; default: break; } return super.onOptionsItemSelected(item); } }
案例二代码:
在下面你的截图中的xml文件中写下面的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:icon="@drawable/ic_launcher" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/action_settings"> <menu > <item android:id="@+id/create_new" android:title="新建文件夹"> </item> <item android:id="@+id/open" android:title="打开文件夹"> </item> </menu> </item> <item android:id="@+id/sys" android:icon="@drawable/ic_launcher" android:orderInCategory="101" android:showAsAction="never" android:title="用户菜单"> <menu> <group android:id="@+id/group1"> <item android:id="@+id/load" android:title="加载文件"> </item> </group> <group android:id="@+id/group2"> <item android:id="@+id/save" android:title="保存文件"> </item> </group> </menu> </item> </menu>
MainActivity.java
package cn.edu.aynu.rjxy; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.create_new: //加载意图 //Intent intent = new Intent(MainActivity.this,LoginActivity.class); //item.setIntent(intent); Toast.makeText(MainActivity.this, "新建文件夹", 0).show(); break; case R.id.load: Toast.makeText(MainActivity.this, "加载文件夹", 0).show(); break; case R.id.open: Toast.makeText(MainActivity.this, "打开文件夹", 0).show(); break; case R.id.save: Toast.makeText(MainActivity.this, "保存文件夹", 0).show(); break; default: break; } return super.onOptionsItemSelected(item); } }
案例三代码:
下面代码的改写位置同上面的一样。
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/edit" android:orderInCategory="100" android:showAsAction="never" android:title="edit"/> <item android:id="@+id/share" android:orderInCategory="100" android:showAsAction="never" android:title="share"/> <item android:id="@+id/delete" android:orderInCategory="100" android:showAsAction="never" android:title="delete"/> </menu>
MainActivity.java
package cn.edu.aynu.rjxy.activity; import java.util.ArrayList; import java.util.List; import cn.edu.aynu.rjxy.activity.R.menu; import android.os.Bundle; import android.R.anim; import android.app.Activity; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()); listview.setAdapter(adapter); //给listview注册上下文菜单 registerForContextMenu(listview); } public List<String> getData(){ List<String> list = new ArrayList<String>(); for (int i = 0; i < 7; i++) { list.add("jack"+i); } return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //加载xml中的上下文菜单 super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.edit: Toast.makeText(MainActivity.this, "edit", 0).show(); break; case R.id.share: Toast.makeText(MainActivity.this, "share", 0).show(); break; case R.id.delete: Toast.makeText(MainActivity.this, "delete", 0).show(); break; default: break; } return super.onContextItemSelected(item); } }
posted on 2016-11-18 19:48 kang_ya_ping 阅读(355) 评论(0) 编辑 收藏 举报