Android : Display an EditText in a menu

2019-02-23 18:34发布

问题:

I'm currently working on a mp3 library for Android. Thing is, I want the user to pick up the name of the playlist he would like to add (useful innit ?). Therefore, I created a context menu on the playlists list which allows the user to choose between 3 options, create, delete, rename. Why I'm trying to do is to display an EditText field if the user clicks on "create", so that he can write the new playlist's name. However, I can't manage to display it. Here are the listeners I made for the menu :

    //creation onLongClickListener : display the menu on long lick
    playlistView.setOnLongClickListener(new OnLongClickListener(){
        public boolean onLongClick(View v) {
            ((Activity) context).openContextMenu(v);
            return true;
    }});

    //contextMenuListener of the view
    playlistView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){

        public void onCreateContextMenu(ContextMenu cMenu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuInflater inflater = ((Activity) context).getMenuInflater();
            inflater.inflate(R.menu.playlist_menu, cMenu);

            lastLongClickedView = v;
    }});

In the menu.xml file I did this :

<item android:id="@+id/playlist_create"
      android:title="Create a playlist"
      >
      <menu>
        <EditText
        android:id="@+id/playlist_choice"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="@string/playlist_name"
        android:lines="1"
        />
      </menu>
</item>

I tried with an item, it's displayed, but the EditText. I assume the mistake I'm making is pretty stupid yet I can't find it :(

Thank you for your attention.

回答1:

Sorry, no way.. you cant define our own View in the menu. the menu main tag has only one subtag i.e,item. using item tag we can only display image or string