常用的菜单
TextView textView;
private TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
textView=findViewById(R.id.textView);
view=findViewById(R.id.popup_tv);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
private void showPopupMenu() {
PopupMenu popupMenu= new PopupMenu(Main4Activity.this,view);
popupMenu.inflate(R.menu.item);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.item1:
view.setTextColor(Color.BLUE);
break;
case R.id.item2:
view.setTextColor(Color.MAGENTA);
break;
case R.id.item3:
view.setTextColor(Color.LTGRAY);
break;
case R.id.item4:
view.setTextColor(Color.BLACK);
break;
}
return false;
}
});
popupMenu.show();
}
});
registerForContextMenu(textView);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.item1:
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.item, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.item, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.item1:
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
public void zz(View view) {
}
- [ ]
- [ ]
- 在这里插入代码片
}