Android开发长按菜单上下文菜单
Posted 啊玉_jspphp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发长按菜单上下文菜单相关的知识,希望对你有一定的参考价值。
安卓开发中长按弹出菜单的创建方法:
1.首先给View注册上下文菜单registerForContextMenu();
2.添加上下文菜单内容onCreateContextMenu():
---可以通过代码动态添加;
---可以加载xml文件中的菜单项;
3.设置菜单点击后响应事件onContextItemSelected();
运行效果图如下:
activity_main.xml代码如下:
<RelativeLayout 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: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="com.example.androidtest.MainActivity" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listview" ></ListView> </RelativeLayout>
MainActivity.java代码如下:
package com.example.androidtest; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; @SuppressLint("NewApi") public class MainActivity extends Activity implements OnClickListener{ Button button1; Button button2; NotificationManager manager; int nid; String[] items={"唱歌","学习","跑步"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showListView(); }
private void showListView(){ ListView listview = (ListView) findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getData()); listview.setAdapter(adapter); this.registerForContextMenu(listview); } @Override public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu,v,menuInfo); //设置Menu显示内容 menu.setHeaderTitle("文件操作"); menu.setHeaderIcon(R.drawable.ic_launcher); menu.add(1,1,1,"复制"); menu.add(1,2,1,"粘贴"); menu.add(1,3,1,"剪切"); menu.add(1,4,1,"重命名"); } @Override public boolean onContextItemSelected(MenuItem item){ switch(item.getItemId()){ case 1: Toast.makeText(MainActivity.this,"点击复制",Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this,"点击粘贴",Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this,"点击剪切",Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(MainActivity.this,"点击重命名",Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } private ArrayList<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i=0;i<5;i++){ list.add("文件"+(i+1)); } return list; } @Override public void onClick(View v) { // TODO Auto-generated method stub }
}
以上是关于Android开发长按菜单上下文菜单的主要内容,如果未能解决你的问题,请参考以下文章
用于TextView的Android TextView空白区域长按上下文菜单,而不是文本选择菜单