如何在longpress上添加删除,共享,取消按钮?

时间:2017-01-25 11:16:28

标签: java android

我是新手编码,现在创建一个Android应用程序,在视频选项卡/列表下有一些视频,我的问题是如何在长按a时添加删除(带通知),共享,取消按钮我要删除的特定视频(来自内部存储)?用例子指导我。在全新的这个:)

的minSdkVersion = “11” targetSdkVersion = “25”

搜索但未找到任何适当的答案,但是..

渴望学习:(

@Override

public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      switch(item.getItemId()) {
         case R.id.deletevideo:
         // add stuff here
            return true;
          case R.id.sharevideo:
            // edit stuff here
                return true;
          case R.id.cancelvideo:
        // remove stuff here
                return true;K
          default:
                return super.onContextItemSelected(item);
      }
}
}

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >


    <item
        android:id="@+id/deletevideo"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/video_delete"/>
    <item
        android:id="@+id/sharevideo"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="@string/video_share"/>
    <item
        android:id="@+id/cancelvideo"
        android:title="@string/video_Cancel"/>

</menu>

1 个答案:

答案 0 :(得分:1)

您所要做的就是添加contextListener:

Java文件

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);

          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu_list, menu);
          return true;


}

@Override
public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      switch(item.getItemId()) {
         case R.id.add:
         // add stuff here
            return true;
          case R.id.edit:
            // edit stuff here
                return true;
          case R.id.delete:
        // remove stuff here
                return true;
          default:
                return super.onContextItemSelected(item);
      }
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">

       <item android:id="@+id/add"
              android:icon="@android:drawable/ic_menu_add"
              android:title="@string/menu_delete" />

      <item android:id="@+id/edit"
              android:icon="@android:drawable/ic_menu_edit"
              android:title="@string/menu_share" />

       <item android:id="@+id/delete"
            android:icon="@android:drawable/my_icon_delete"
             android:title="@string/menu_cancle" />

</menu>