Android中的设置按钮:长按或点击菜单键
1.长按选项:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" > <TextView
android:id="@+id/menutext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一种方法创建菜单"
android:textSize="20sp" /> <TextView
android:id="@+id/menutext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二种方法创建菜单"
android:textSize="20sp" /> <ListView
android:id="@+id/menulist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" >
</ListView> </LinearLayout>
实现过程:
private ListView list;
private TextView lv; String[] str = { "TextView", "EditView", "Toast(消息框)" }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu); list = (ListView) findViewById(R.id.menulist);
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, str);
list.setAdapter(arr); list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.clear();
menu.clearHeader();
menu.setHeaderIcon((R.drawable.ic_launcher));
menu.setHeaderTitle("我是ListView");
menu.add(3, 6, 0, "删除");
menu.add(3, 7, 1, "修改");
}
});
// 第一种方法
TextView tv = (TextView) findViewById(R.id.menutext1);
this.registerForContextMenu(tv);
// 第二种方法
lv = (TextView) findViewById(R.id.menutext2);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长度-ContextMenu");
menu.add(0, 1, 0, "复制");
SubMenu sb = menu.addSubMenu("查找");
sb.add(1, 2, 0, "按id查找");
sb.add(1, 3, 0, "按名称查找");
}
});
}
2.长按菜单项:
private ListView list;
private TextView lv; String[] str = { "TextView", "EditView", "Toast(消息框)" }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu); list = (ListView) findViewById(R.id.menulist);
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, str);
list.setAdapter(arr); list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.clear();
menu.clearHeader();
menu.setHeaderIcon((R.drawable.ic_launcher));
menu.setHeaderTitle("我是ListView");
menu.add(3, 6, 0, "删除");
menu.add(3, 7, 1, "修改");
}
});
// 第一种方法
TextView tv = (TextView) findViewById(R.id.menutext1);
this.registerForContextMenu(tv);
// 第二种方法
lv = (TextView) findViewById(R.id.menutext2);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长度-ContextMenu");
menu.add(0, 1, 0, "复制");
SubMenu sb = menu.addSubMenu("查找");
sb.add(1, 2, 0, "按id查找");
sb.add(1, 3, 0, "按名称查找");
}
});
}
Over