您的位置:首页 > 其它

menu

2015-07-24 23:17 323 查看
一、选项菜单的设计:

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: