menu

一、选项菜单的设计:

1.   编写String.xml文件设置几个字符川供选项使用。

<string name="exitMenu">退出</string>
<string name="aboutMenu">关于</string>
<string name="shareMenu">分享</string>


2.   编写一个菜单布局main.xml文件,作为选项列表。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.hc.androidmenu.MainActivity">
<!--android:orderInCategory="100"代表排列顺序,最大的排在最下面 -->
    <item
        android:id="@+id/exit"
        android:orderInCategory="100"
        android:title="@string/exitMenu"/>
    <item
        android:id="@+id/about"
        android:orderInCategory="80"
        android:title="@string/aboutMenu"/>
    <item
        android:id="@+id/share"
        android:orderInCategory="90"
        android:title="@string/shareMenu"/>
 
</menu><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>

3.   编写acticity.java文件,重写onCreateOptionsMenu(),内容如下:

@Override
   public booleanonCreateOptionsMenu(Menu menu) {
      //将xml绑定到菜单资源
      getMenuInflater().inflate(R.menu.main,menu);
      //返回出表示处理加载完毕
      return true;
}

4.重写onOptionsItemSelected(),内容如下:

 

public boolean onOptionsItemSelected(MenuItemitem) {
      //取出item的Id
      int id = item.getItemId();
      switch (id) {
      case R.id.exit:
         System.exit(0);
         break;
      case R.id.share:
         Intent intent = new Intent();
         //设置动作
         intent.setAction(Intent.ACTION_SEND);
         //设置类型
         intent.setType("text/plain");
         //设置参数
         intent.putExtra(Intent.EXTRA_TEXT,"newhc");
         //用包管理器,查找手机里有无相关的activity来使用
         if(intent.resolveActivity(getPackageManager())!=null){
            startActivity(intent);
         }
         break;
      case R.id.about:
         Toast.makeText(MainActivity.this,"yecats的软件", Toast.LENGTH_SHORT).show();
         break;
      }
      //返回true表示处理完毕
      return true;
}

二、上下文菜单的设计:

 

三、为listView控件设置上下文菜单:

1.   设计出listView列表控件。

<ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
   </ListView>


 

//activity.java函数中
registerForContextMenu(listview);
      //设置数据
      names = newString[100];
      for(inti =0; i<names.length;i++){
         names[i] ="people:"+i;
      }
      ListAdapter adapter = newArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names);
      listview.setAdapter(adapter);


2.   注册listView,然后长按时得到菜单,单击菜单取出每一项的内容。

if(item.getMenuInfo()!=null){
         AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
         int position = menuInfo.position;
         Toast.makeText(MainActivity.this,names[position], Toast.LENGTH_SHORT).show();
        
      }


 

 

 

四、单击弹出菜单,并可设置弹出位置:

1.编写一个按钮,设置单击事件。

<Button
        android:id="@+id/button2"
        android:onClick="showMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击弹出菜单"/>


2.编写单击事件方法。

//单击弹出菜单
   public voidshowMenu(View view ){
      //创建弹出菜单,围绕button,默认在下方显示
      PopupMenu pop = newPopupMenu(this,button);
      //将pop封装成menu
      getMenuInflater().inflate(R.menu.context_menu,pop.getMenu());
      //处理pop选中事件
      pop.setOnMenuItemClickListener(newOnMenuItemClickListener() {
        
         @Override
         public booleanonMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            switch (id) {
            case R.id.delete:
                Toast.makeText(MainActivity.this,"delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.editor:
                Toast.makeText(MainActivity.this,"editor", Toast.LENGTH_SHORT).show();
                break;
            case R.id.modify:
                Toast.makeText(MainActivity.this,"modify", Toast.LENGTH_SHORT).show();
                break;
            case R.id.cut:
                Toast.makeText(MainActivity.this,"cut", Toast.LENGTH_SHORT).show();
                break;
            }
            return true;
         }
      });
      //弹出
      pop.show();
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值