android 为组件添加contextMenu上下文菜单

package com.example.fragmentNavigation2.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;
import com.example.fragmentNavigation2.R; public class WeiXinFragment extends Fragment { private Button button; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.wei_xin_fragment, container, false);
button = (Button) view.findViewById(R.id.press_button);
registerForContextMenu(button);
return view;
} @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
new MenuInflater(getActivity()).inflate(R.menu.context_menu, menu);
menu.setHeaderTitle("请选择你喜欢的网站");
super.onCreateContextMenu(menu, v, menuInfo);
} /**
* 相应本fragment对应的上下文菜单事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_item:
Toast.makeText(getActivity(), "重庆国际矿产开发协会", 1).show();
break;
case R.id.second_item:
Toast.makeText(getActivity(), "重庆雨人网络工作室", 1).show();
break;
}
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/first_item" android:title="第一个选项"></item>
<item android:id="@+id/second_item" android:title="第二个选项"></item>
<item android:title="第三个选项"></item>
</menu>
上一篇:java 创建string对象机制 字符串缓冲池 字符串拼接机制


下一篇:cs224w学习笔记(02)传统图机器学习方法