ContextMenu上下文菜单 PopupMenu弹出式菜单 小例子

 

 

 

___________________________________________________________________________________________

res/menu中代码:

 

popup_menu.xml

 



   

   



 

————————————————————————————————————————————

 

activity类代码

 

 

public class Main2Activity extendsAppCompatActivity {

 

   private TextView textView;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main2);

       textView = (TextView) findViewById(R.id.textView);

       registerForContextMenu(textView);//把上下文菜单注册到某一个组件上,那么在长按该组件会弹出上下文菜单

    }

 

   @Override

   public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {

       super.onCreateContextMenu(menu, v, menuInfo);

       System.out.println(menu);

       System.out.println(v);

       System.out.println(menuInfo);

       getMenuInflater().inflate(R.menu.context_menu,menu);

    }

 

   @Override

   public boolean onContextItemSelected(MenuItem item) {

//       item.getTitle();

        System.out.println(item.getMenuInfo());

       AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

       System.out.println(info.targetView);

       switch (item.getItemId()){

           case R.id.color_red:

               textView.setBackgroundColor(Color.RED);

                break;

           case R.id.color_green:

               textView.setBackgroundColor(Color.GREEN);

                break;

           case R.id.color_yellow:

                textView.setBackgroundColor(Color.YELLOW);

                break;

       }

       return super.onContextItemSelected(item);

    }

}




public class Main4Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
    }
    public void showPopupMenuClick(View view){

        //创建弹出式菜单(context,要把菜单显示在哪个组件的旁边)
        PopupMenu popupMenu = new PopupMenu(this,view);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.color_red:
                        Toast.makeText(Main4Activity.this, "red", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.color_green:
                        Toast.makeText(Main4Activity.this, "green", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.color_yellow:
                        Toast.makeText(Main4Activity.this, "yellow", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
    }
}


你可能感兴趣的:(功能实现范例)