0

我的问题就像它在描述中所说的那样,上下文菜单没有出现在用户的“触摸并按住”上。我有一种感觉,它可能是我放置的地方registerForContextMenu
这是我的主要活动:

import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerForContextMenu(getListView());
setContentView(R.layout.activity_main);
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }
}
4

2 回答 2

1

尝试切换这两行

registerForContextMenu(getListView());
setContentView(R.layout.activity_main);

所以应该是

setContentView(R.layout.activity_main);
registerForContextMenu(getListView());

你的感觉是对的。你ListView在你的layout,所以你需要先给它充气,然后再注册View

于 2013-08-06T18:35:39.233 回答
0

我假设用户的“触摸并按住”意味着“长按”?

如果是这样,请尝试查看此帖子以查看您的问题是否相似。也许您需要使用 setOnLongClickListener ...

于 2013-08-06T18:33:42.977 回答