gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:16:54 25 4
gpt4 key购买 nike

我很难理解 Android 上下文菜单的工作原理。我正在编写一个简单的程序,它将在 ListView 中显示不同类型冰淇淋的列表。当用户“点击并按住每种类型时,应出现一个上下文菜单,其中包含 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)的新手,并且由于害怕公开的技术人员 mock 或被视为利用社区而不敢提问。我只是希望在某个时候我能够将它转给其他人。

谢谢!

这是我的代码:

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));
}


}

最佳答案

简短的回答是将 menuInfo 转换为 AdapterContextMenuInfo 对象,然后访问其公共(public)位置成员变量

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

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

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

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

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

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

关于android - 如何使用 Android 上下文菜单(点击并按住),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4019337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com