Android上的上下文菜单单击

4

我刚开始学习Android开发,试图在我的应用程序中添加上下文菜单。我知道默认情况下需要长按按钮才能打开上下文菜单。但是我需要让它在单击时出现。我尝试了stackoverflow上的所有其他解决方案,但都没有真正帮助到我。

我在下面发布了我的代码。请告诉我需要做哪些修改使其可以工作。

public class ThirdActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_layout);
        confirmButton = (Button) findViewById(R.id.confirmButton);
        registerForContextMenu(confirmButton);
}

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Select Menu");
    menu.add(0, v.getId(), 0, "Action 1");
}


public boolean onContextItemSelected(MenuItem item) {

      if (item.getTitle() == "Action 1") {
        //do something
    }
}
3个回答

9
just :

public class ThirdActivity extends ActionBarActivity { 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_layout);
        confirmButton = (Button) findViewById(R.id.confirmButton);
    confirmButton .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            confirmButton .performLongClick();
        }
    });
        registerForContextMenu(confirmButton); 
} 

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Select Menu");
    menu.add(0, v.getId(), 0, "Action 1");
} 


public boolean onContextItemSelected(MenuItem item) {

      if (item.getTitle() == "Action 1") {
        //do something 
    } 
} 

尝试将隐藏按钮注册为ContextMenuBtn。当单击可见按钮时,隐藏按钮执行performLongClick。 - tiny sunlight

0

你可以简单地写:

 confirmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.showContextMenu();
        }
    });

0
我不确定从哪个Android版本开始,当调用view.showContextMenu()时行为发生了变化。我在Android 13 (API 33)上,它显示一个全屏对话框,位于屏幕中间,而不是通常在点击位置出现的上下文菜单。我甚至尝试调用view.performLongClick(),结果再次显示了新的UI样式,这暗示着Google的某个人真的想要将通常的上下文菜单保留给实际的长按事件!
我几乎放弃了,直到我发现showContextMenu()有一个重载方法showContextMenu(x, y),它接受x和y坐标,并且似乎会创建从指定的x和y坐标生成的通常的上下文菜单。
至于如何获取点击位置的x和y坐标,这是我使用的方法:
private var lastTouchedPositionX: Float = 0.0f
private var lastTouchedPositionY: Float = 0.0f
itemView.setOnTouchListener { view, motionEvent ->
    when (motionEvent.action) {
        MotionEvent.ACTION_DOWN -> {
            lastTouchedPositionX = motionEvent.x
            lastTouchedPositionY = motionEvent.y
        }
        MotionEvent.ACTION_UP -> {
            if (motionEvent.x == lastTouchedPositionX && motionEvent.y == lastTouchedPositionY) {
                // item is clicked
                itemView.showContextMenu(lastTouchedPositionX, lastTouchedPositionY)
            }
            lastTouchedPositionX = 0.0f
            lastTouchedPositionY = 0.0f
        }
    }
    true
}

我使用了setOnTouchListener,因为setOnClickListener不返回x和y坐标,而setOnTouchListener返回,我假设如果ACTION_DOWN和ACTION_UP都发生在相同的坐标上,那就意味着发生了点击事件,所以我们调用showContextMenu(x, y)。
在Android 13上似乎运行得很好,但没有测试过旧版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接