Button上的上下文菜单长按?

时间:2014-02-27 13:06:21

标签: android onlongclicklistener android-contextmenu

如何让我的应用在长项目上显示上下文菜单点击? 我写了这段代码,它正在运行。 如何在点击某个按钮时显示它?

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textColor="#04B404" />

代码:

TextView tv=(TextView)findViewById(R.id.tv);
tv.setOnCreateContextMenuListener(this);


@Override 
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuinfo) {
    super.onCreateContextMenu(menu, view, menuinfo);
    menu.setHeaderTitle("Set as");
    menu.add(menu.FIRST, Menu.NONE, 0, "Set as Wallpaper");
    menu.add(menu.FIRST+1, Menu.NONE, 0, "Download");
    menu.add(menu.FIRST+2, Menu.NONE, 0, "Info);

}

5 个答案:

答案 0 :(得分:3)

试用此代码:

public class MainActivity extends ListActivity {  

  private String[] items;
  private List<String> list;
  private  ArrayAdapter<String> adapter;
  private int position;
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.activity_main);  
       fillData();  
       registerForContextMenu(getListView());
  }  
  private void fillData() {  
       items = new String[] {"Monday", "Tuesday", "Wednesday",   
                        "Thursday", "Friday", "Saturday", "Sunday"};
       list = new ArrayList<String>();
       Collections.addAll(list, items);
       adapter = new ArrayAdapter<String>(this, R.layout.row,
       R.id.r_text, list);  
       setListAdapter(adapter);            
  }  

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater m = getMenuInflater();
    m.inflate(R.menu.our_context_menu, menu);
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.delete_item:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            position = (int) info.id;
            list.remove(position);
            this.adapter.notifyDataSetChanged();
            return true;
  }
  return super.onContextItemSelected(item);
}

}

Activity.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

</LinearLayout> 

<强> row.xml:

<?xml version="1.0" encoding="utf-8"?>  
<TextView   
      xmlns:android="http://schemas.android.com/apk/res/android"  
      android:textIsSelectable="true"  
      android:id="@+id/r_text"   
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent"  
      android:padding="10dip"  
      android:textColor="#ffffff"  
      android:textSize="17sp"/>  

现在在menu中创建一个res文件夹并将此文件放入其中。

上下文菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
      android:id="@+id/delete_item"
      android:title="@string/delete_string"/>    
</menu>

答案 1 :(得分:1)

您可以使用registerForContextMenu(View v)方法,该方法采用您要注册ContextMenu的视图。

因此,如果您的Button被称为myButton,

 registerForContextMenu(myButton);

因此,如果您希望长按一下,只需添加onLongClickListener并使用上面的注册按钮。

继承人example.

答案 2 :(得分:0)

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:visibility="gone"
        android:textColor="#04B404" />

现在拿一个按钮,这就是你所做的wd文本视图就是这样做了,点击那个按钮就像这样做了

Button dialogButtonCancel = (Button) findViewById(R.id.dialogButtonCancel);

                    dialogButtonCancel.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        textview.setVisibility(View.VISIBLE);                              


                        }
                    });

答案 3 :(得分:0)

XML代码

<Button
            android:id="@+id/btnFb"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1"
            android:text="Button" />

Java代码

btnFb.setOnClickListener(FbListenser);
private OnClickListener FbListenser = new OnClickListener() {
    public void onClick(View v) {
                    PopupMenu popupMenu = new PopupMenu(activity.getApplicationContext(), v);
                        List<String> lstFb = new List<String>();
                        for (int i = 0; i < lstFb.size(); i++) {
                                popupMenu.getMenu().add(lstFb.get(i));
                        }
                        popupMenu.setOnMenuItemClickListener(new                         PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                                    String Value = item.toString();
                         }
                });

        popupMenu.show();
        }
};

答案 4 :(得分:0)

调用registerForContextMenu(tv);并将要附加上下文菜单的视图传递给它。