java 弹出菜单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 弹出菜单相关的知识,希望对你有一定的参考价值。


public class MainActivity extends AppCompatActivity {

    private final static int ONE = 1;
    private final static int TWO = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView myAnchor = findViewById(R.id.tv);
        // Standard popup: 
        //final PopupMenu popupMenu = new PopupMenu(this, myAnchor);
        // Changes position of the popup menu:
        final PopupMenu popupMenu = new PopupMenu(this, myAnchor, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);
        popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Item 1");
        popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "Item 2");

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case ONE:
                        Toast.makeText(MainActivity.this, "One", Toast.LENGTH_SHORT).show();
                        break;
                    case TWO:
                        Toast.makeText(MainActivity.this, "Two ", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });


        myAnchor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                popupMenu.show();
            }
        });

以上是关于java 弹出菜单的主要内容,如果未能解决你的问题,请参考以下文章

java 弹出菜单

Delphi XE7实现的任意位置弹出菜单

弹出菜单的创建与使用

如何防止在 JavaFX 中按下 SPACE 键时关闭 AutoCompleteCombobox 弹出菜单

Android仿IOS底部弹出选择菜单ActionSheet

android中RecyclerView控件实现长按弹出PopupMenu菜单功能