Android:长按文本视图后,我需要弹出上下文菜单

Posted

技术标签:

【中文标题】Android:长按文本视图后,我需要弹出上下文菜单【英文标题】:Android: After long pressing a textview I need a Pop up Context Menu 【发布时间】:2017-05-08 19:49:58 【问题描述】:

我使用 Recyclerview 扩展了 Fragment 类。长按 textView 后我需要一个弹出菜单。我已经显示了下面的代码。没有错误,所以我不知道再看哪里了。

public class RecyclerViewFragment  extends Fragment implements View.OnCreateContextMenuListener 

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return recyclerView;
    



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener 
        public ImageView avator;
        public TextView name;
        public TextView description;
        public TextView num;
        public View divider;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) 
            super(inflater.inflate(R.layout.listing_view, parent, false));
            avator = (ImageView) itemView.findViewById(R.id.listing_avatar);
            name = (TextView) itemView.findViewById(R.id.listing_title);
            description = (TextView) itemView.findViewById(R.id.listing_desc);
            num = (TextView) itemView.findViewById(R.id.listing_num) ;
            divider= itemView.findViewById(R.id.listing_div);
            itemView.setOnCreateContextMenuListener(this);
        

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 
            if (v.getId()==R.id.list) 
                //AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
                menu.setHeaderTitle("Action");
                String[] menuItems = getResources().getStringArray(R.array.arr_menu);
                for (int i = 0; i<menuItems.length; i++) 
                    menu.add(Menu.NONE, i, i, menuItems[i]);
                
            
        
    

【问题讨论】:

回收器视图如何扩展片段类? 【参考方案1】:

tv.setOnLongClickListener(new OnLongClickListener()

    @Override
    public boolean onLongClick(View v) 
        // TODO Auto-generated method stub
        return false;
    

);

使用 OnLongClickListener 代替 OnClickListner

https://developer.android.com/guide/topics/ui/menus.html

【讨论】:

【参考方案2】:

希望对你有帮助。

onCreate()

registerForContextMenu(textView1);

@Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
      
            super.onCreateContextMenu(menu, v, menuInfo);  
            menu.setHeaderTitle("Select The Action");    
            menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title   
            menu.add(0, v.getId(), 0, "SMS");   
       
    @Override    
    public boolean onContextItemSelected(MenuItem item)    
            if(item.getTitle()=="Call")  
                Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
                
            else if(item.getTitle()=="SMS")  
                Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
            else  
               return false;  
                
          return true;    
          

【讨论】:

以上是关于Android:长按文本视图后,我需要弹出上下文菜单的主要内容,如果未能解决你的问题,请参考以下文章

android 长按事件 和 长按弹出上下文菜单如何处理

text Android Edittext长按没有弹出上下文菜单

如何从listview android的主列表中获取所选项目的文本

长按网页视图的水平菜​​单充气机

Android Studio中上下文菜单的使用

用于TextView的Android TextView空白区域长按上下文菜单,而不是文本选择菜单