当前位置:网站首页>ContextMenu(上下文菜单)
ContextMenu(上下文菜单)
2022-07-27 17:02:00 【Ashurol】
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowListView();
}
/*
* *显示ListView
*/
private void ShowListView()
{
//在主布局文件里记得添加LIstView控件
ListView listview=(ListView) findViewById(R.id.listview);
ArrayAdapter<String> adp=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getString());
listview.setAdapter(adp);
this.registerForContextMenu(listview);
}
/*
* 创建列表适配器需要的列表项字符串集合
*/
private ArrayList<String> getString()
{
ArrayList<String> list=new ArrayList<String>();
for(int i=0;i<5;i++)
{
list.add("菜单"+i);
}
return list;
}
/*
* 上下文菜单内容添加方法
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("上下文");
menu.setHeaderIcon(R.drawable.ic_launcher);
//通过Menu里的布局文件来添加菜单项
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
//动态添加菜单项
// menu.add(1, 1, 1, "复制");
// menu.add(1, 2, 1, "黏贴");
// menu.add(1, 3, 1, "剪切");
// menu.add(1, 4, 1, "重命名");
super.onCreateContextMenu(menu, v, menuInfo);
}
/*
* 添加菜单选项点击事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
//点击判断是由于用两种添加菜单项的方式,所以对应两种ID判断
// case 1:
// Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show();
// break;
// case 2:
// Toast.makeText(this, "点击了黏贴", Toast.LENGTH_SHORT).show();
// break;
// case 3:
// Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show();
// break;
// case 4:
// Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show();
// break;
case R.id.action_settings1:
Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings2:
Toast.makeText(this, "点击了黏贴", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings3:
Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings4:
Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
//menu文件夹里的main文件<pre name="code" class="html"><menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.xin.sxw.MainActivity" >
<item
android:id="@+id/action_settings1"
android:orderInCategory="100"
android:title="复制"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:title="黏贴"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings3"
android:orderInCategory="100"
android:title="剪切"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings4"
android:orderInCategory="100"
android:title="重命名"
app:showAsAction="never"/>
</menu>
边栏推荐
- [basic knowledge of deep learning - 43] concept of odds ratio
- Incluxdb series (III) detailed explanation of incluxdb configuration file
- C language: 12. GDB tool debugging C program
- 成本高、落地难、见效慢,开源安全怎么办?
- The first in the field of mobile phone chip design in the world! Ziguang zhanrui won the international certification of tmmi4
- 台积电5nm即将量产:苹果A14独占7成产能,华为麒麟1020拿下3成
- 揭秘高通超声波指纹被“贴膜破解”之谜
- 英特尔未来10年工艺路线图曝光:2029年推出1.4nm工艺!如何实现?
- 嵌入式C语言结构体
- HDU1171_ Big event in HDU [01 backpack]
猜你喜欢

5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress

Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased

C language: 13. Pointer and memory

A low code development platform that brings high-value user experience

c语言:c语言代码风格

【深度学习基础知识 - 42】逻辑回归详解

c语言:10、输入流,输出流,错误流

电商商城小程序项目完整源码(微信小程序)

【深度学习基础知识 - 43】优势比的概念

C language: 8. Makefile preparation
随机推荐
C language: 7. How to use C language multi source files
反超华为?爱立信已拿下超过75份5G商用合同
TSMC 5nm is about to mass produce: Apple A14 monopolizes 70% of the production capacity, and Huawei Kirin 1020 takes 30%
三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
【深度学习基础知识 - 49】Kmeans
Samsung will promote a number of risc-v architecture chips, and 5g millimeter wave RF chips will be the first to be adopted
Dry goods of technical practice | preliminary exploration of large-scale gbdt training
C language: 6. Simple use and precautions of pointer
C language: 15. Structure
jvisualvm的使用
【深度学习基础知识 - 45】机器学习中常用的距离计算方法
I want to consult. Our maxcompute spark program needs to access redis, development environment and production environment redis
S32K系列芯片--简介
RadioGroup(单选框)
C language: 10. Input stream, output stream, error stream
坚持软硬一体化,一恒科发力智能化教育机器人市场
The valuation exceeds 15.6 billion yuan! Huaqin communication completed the round B financing of 1billion yuan! Qualcomm venture capital, Intel Capital led investment
【深度学习基础知识 - 41】深度学习快速入门学习资料
【深度学习基础知识 - 42】逻辑回归详解
查看宝塔PHP扩展目录