如何使用 Android 上下文菜单(点击并按住)

发布于 2024-09-28 20:21:52 字数 4090 浏览 0 评论 0原文

我很难理解 Android 上下文菜单的工作原理。我正在编写一个简单的程序,它将在列表视图中显示不同类型冰淇淋的列表。当用户“点击并按住每种类型时,应该会出现一个上下文菜单,其中包含 4 个选项,询问他们想要显示有关该冰淇淋的哪些信息(图片、成分、订单、其他)。

我不明白如何制作这样每个列表项的上下文菜单都会提供适当的信息(与用户点击并按住的任何列表项相关的信息)

。不处理为每个列表项显示不同信息的问题,我看到在给定的代码中,我的教授甚至在 onCreateContextMenu() 的参数内的注释中给出了提示:

public void onCreateContextMenu( //called each time the context menu is requested
        ContextMenu menu,   //the ContextMenu itself
        View v, //The view for which the context menu is being built
        ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

但我仍然不确定我能够做什么。处理这个参数,我已经旋转了很长时间了,stackoverflow 上至少有另一个线程解决了这个问题,但它并没有帮助我更接近地理解这里发生的事情

。非常感谢您的帮助,因为我是 Android(以及一般的 OOP)新手,并且有点犹豫是否要提出问题,因为担心公众技术人员嘲笑或被视为利用社区。我只是希望在某个时候我能够把钱转给别人。

谢谢!

这是我的代码:

package CS285.Assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class IceCreamMenu extends ListActivity {
    TextView selection;
    ImageView imgView;

    String[] items={"Butter Pecan", "Chocolate", 
            "Coffee", "Mango", 
            "Rocky Road","Strawberry", 
            "Vanilla"};

    int[] images={R.drawable.butterpecan,
            R.drawable.choclate,
            R.drawable.coffee,
            R.drawable.mango,
            R.drawable.rockyroad,
            R.drawable.strawberry,
            R.drawable.vanilla};

    public static final int IMG_ID = 0;
    public static final int INGR_ID = 1;
    public static final int ORDER_ID = 2;
    public static final int OTHER_ID = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        imgView = (ImageView)findViewById(R.id.Image);

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, 
                                                items));
        registerForContextMenu(getListView()); //indicate that the ListView has a context menu.

    }

    //event handling for ListItemClick events
    public void onListItemClick(ListView parent, View v,
                        int position, long id) {

        selection.setText(items[position]);

    }

    @Override
    public void onCreateContextMenu( //called each time the context menu is requested
            ContextMenu menu,   //the ContextMenu itself
            View v, //The view for which the context menu is being built
            ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

            populateMenu(menu);

    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
        menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
        menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
        menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");

    }

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case IMG_ID:

            imgView.setImageResource(images[0]);

            return(true);

        case INGR_ID:


            return(true);

        case ORDER_ID:


            return(true);

        case OTHER_ID:


            return(true);
        }

        return(false);
    }

    //called whenever an item in a ContextMenu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
        return(applyMenuChoice(item) ||  
        super.onContextItemSelected(item));
    }


}

I am having a lot of trouble understanding how the Android context menu works. I am writing a simple program which will display a list of different types of ice cream in a list view. When the user to "taps and holds over each type, a context menu should appear with 4 options asking what information they want to be displayed about that ice cream (picture, ingredients, order, other).

I don't understand how to make it so that each list item's context menu gives the appropriate info (the info associated with whichever list item the user tapped and held).

I am working from an example given by my teacher, which has some of the functionality I am after, but does not deal with the issue of displaying different info for each list item. I see that in the given code, my prof even gave a hint in the comments within the parameters of onCreateContextMenu():

public void onCreateContextMenu( //called each time the context menu is requested
        ContextMenu menu,   //the ContextMenu itself
        View v, //The view for which the context menu is being built
        ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

But I am still unsure of what I am able to do with this parameter, and I've been spinning my wheels for a long time now. There is at least one other thread on stackoverflow that addresses this question, but it did not help me get any closer to understanding what's going on here.

I really appreciate the help, as I am new to Android (and OOP generally) and a little hesitant to ask questions for fear of public techie ridicule or being seen as taking advantage of the community. I just hope that at some point I'll be able to pay it forward to someone else.

Thanks!

Here's my code:

package CS285.Assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class IceCreamMenu extends ListActivity {
    TextView selection;
    ImageView imgView;

    String[] items={"Butter Pecan", "Chocolate", 
            "Coffee", "Mango", 
            "Rocky Road","Strawberry", 
            "Vanilla"};

    int[] images={R.drawable.butterpecan,
            R.drawable.choclate,
            R.drawable.coffee,
            R.drawable.mango,
            R.drawable.rockyroad,
            R.drawable.strawberry,
            R.drawable.vanilla};

    public static final int IMG_ID = 0;
    public static final int INGR_ID = 1;
    public static final int ORDER_ID = 2;
    public static final int OTHER_ID = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        imgView = (ImageView)findViewById(R.id.Image);

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, 
                                                items));
        registerForContextMenu(getListView()); //indicate that the ListView has a context menu.

    }

    //event handling for ListItemClick events
    public void onListItemClick(ListView parent, View v,
                        int position, long id) {

        selection.setText(items[position]);

    }

    @Override
    public void onCreateContextMenu( //called each time the context menu is requested
            ContextMenu menu,   //the ContextMenu itself
            View v, //The view for which the context menu is being built
            ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

            populateMenu(menu);

    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
        menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
        menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
        menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");

    }

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case IMG_ID:

            imgView.setImageResource(images[0]);

            return(true);

        case INGR_ID:


            return(true);

        case ORDER_ID:


            return(true);

        case OTHER_ID:


            return(true);
        }

        return(false);
    }

    //called whenever an item in a ContextMenu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
        return(applyMenuChoice(item) ||  
        super.onContextItemSelected(item));
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-10-05 20:21:53

简短的答案是将menuInfo转换为AdapterContextMenuInfo对象,然后访问其公共position成员变量

selectedPostion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;

selectedPostion的值是ListView中被长按的位置。

Android 开发人员指南中的这篇文章对如何使用 ContextMenuInfo 对象进行了精彩的描述,必须阅读:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

正如您在 onCreate() 方法中看到的,您的 ListView 正在注册本身响应长按并生成一个上下文菜单。因此,当您长按 ListView 中的某个视图项时,ListView 会为您的 onCreateContextMenu 方法提供 menuInfo 参数。此参数包含有关如何生成 ContextMenu 的信息。

http://developer.android.com/reference/android/view/ContextMenu .ContextMenuInfo.html

ListView 扩展了 AdapterView 类。其中有 AdpterView.AdapterContextMenuInfo 子类。每当长按 ListView 对象时,它都会设置此参数,其中包含有关长按哪个 View 对象的信息。然后该对象被传递给 onCreateContextMenu(...) 方法。

http://developer.android.com/reference/android/widget/AdapterView .AdapterContextMenuInfo.html

The short answer is cast menuInfo to an AdapterContextMenuInfo object and then access its public position member variable

selectedPostion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;

The value of selectedPostion is the position in the ListView that was long-clicked.

This article in the Android Developer Guide has an excellent description of how to work with the ContextMenuInfo object, a must read:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

As you can see in your onCreate() method your ListView is registering itself to respond to long-clicks and generate a ContextMenu. So when you long-click on a View item in the ListView, it is the ListView that is providing your onCreateContextMenu method with the menuInfo parameter. This parameter contains information about how the ContextMenu was generated.

http://developer.android.com/reference/android/view/ContextMenu.ContextMenuInfo.html

The ListView extends the AdapterView class. Which has AdpterView.AdapterContextMenuInfo subclass. Whenever the ListView object is long clicked, it sets up this parameter with information about what View object was long clicked. This object then gets passed to the onCreateContextMenu(...) method.

http://developer.android.com/reference/android/widget/AdapterView.AdapterContextMenuInfo.html

陪你到最终 2024-10-05 20:21:53

您无法在上下文菜单上执行点击并按住功能。听起来你想要的是一个对话框。

You can't do tap and hold functionality on a context menu. It sounds like what you want is a dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文