|
目录结构:
第一步:
/ContextMenuDemo/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">上下文菜单</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="basketball">篮球</string>
<string name="football">足球</string>
<string name="volleyball">排球</string>
<string name="gender">性别</string>
<string name="hobby">爱好</string>
<string name="genderEdit">输入性别</string>
<string name="hobbyEdit">输入爱好</string>
</resources>
复制代码
第二步:
/ContextMenuDemo/res/layout/context_menu_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 性别 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/gender"
android:width="100dip"
android:textSize="24dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/genderEdit"
android:text="@string/genderEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- 爱好 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/hobby"
android:width="100dip"
android:textSize="24dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/hobbyEdit"
android:text="@string/hobbyEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
复制代码
第三步:
/ContextMenuDemo/src/com/mycontextmenu/demo/MenuFinalValues.java
package com.mycontextmenu.demo;
public interface MenuFinalValues {
public static final int MALE = 1;
public static final int FEMALE = 2;
public static final int BASKETBALL = 3;
public static final int FOOTBALL = 4;
public static final int VOLLEYBALL = 5;
}
复制代码
第四步:
/ContextMenuDemo/src/com/mycontextmenu/demo/ContextMenuActivity.java
package com.mycontextmenu.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import static com.mycontextmenu.demo.MenuFinalValues.*;
public class ContextMenuActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.context_menu_layout);
//需为EditText注册ContextMenu,否则当长按输入框时,只能弹出系统自带的
//ContextMenuActivity中的onCreateContextMenu未执行到
this.registerForContextMenu(findViewById(R.id.genderEdit));
this.registerForContextMenu(findViewById(R.id.hobbyEdit));
}
/*
* 只有当控件注册了才调用此方法,当长按2秒后会弹出菜单项
* 不支持快捷键,起菜单选项不能附带图标
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
//清除系统所有系统自带的菜单项
//如果只想清除部分,可以使用menu.removeItem(系统自带菜单项id)
//注:系统自带菜单项ID,请参考http://developer.android.com/reference/android/R.id.html
//如,删除全部选择 menu.removeItem(android.R.id.selectAll);
menu.clear();
if(view == findViewById(R.id.genderEdit)) {
menu.setHeaderIcon(R.drawable.gender);
menu.setHeaderTitle(R.string.gender);
//如果该菜单项里面还未添加菜单,此时菜单项是隐藏的
//添加了MALE后会显示出来
menu.add(0, MALE, 0 ,R.string.male);
menu.add(0, FEMALE, 0, R.string.female);
} else if(view == findViewById(R.id.hobbyEdit)) {
menu.setHeaderIcon(R.drawable.basketball);
menu.setHeaderTitle(R.string.hobby);
//无法为菜单项添加图标icon(即setIcon无效),android2.2不支持
//但是可以通过其他途径添加图标,
//可参考此处https://code.google.com/p/android-icon-context-menu/
//http://www.tanisoft.net/2010/09/android-context-menu-with-icon.HTML
menu.add(0, BASKETBALL, 0, R.string.basketball);
menu.add(0, FOOTBALL, 0, R.string.football);
menu.add(0, VOLLEYBALL, 0, R.string.volleyball);
}
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
EditText editText = null;
switch(menuItem.getItemId()) {
case MALE:
case FEMALE:
editText = (EditText) this.findViewById(R.id.genderEdit);
break;
case BASKETBALL:
case FOOTBALL:
case VOLLEYBALL:
editText = (EditText) this.findViewById(R.id.hobbyEdit);
break;
}
editText.setText(menuItem.getTitle());
return true;
}
}
复制代码
第五步:
/ContextMenuDemo/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycontextmenu.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContextMenuActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
复制代码
效果图:
程序运行初始状态
性别输入框长按2秒后弹出菜单
爱好输入框长按两秒,然后选中足球
菜单选中后,输入框显示所选中的内容
ContextMenuDemo.rar (86.15 KB, 下载次数: 2) |
|