下面列出了android.widget.PopupMenu#inflate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onAppMenu(@NonNull View sender) {
final PopupMenu popupMenu = new PopupMenu(this, sender);
popupMenu.inflate(R.menu.app);
popupMenu.setOnMenuItemClickListener(item -> {
//noinspection SwitchStatementWithTooFewBranches
switch (item.getItemId()) {
case R.id.exit:
ITag.close();
Waytoday.stop();
ITagsService.stop(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask();
} else {
finishAffinity();
}
new Handler(getMainLooper()).postDelayed(() -> System.exit(0), 5000);
break;
}
return true;
});
popupMenu.show();
}
private void showMessagePopup(MessageModel message, int position) {
View item = binding.rvUsers.getChildAt(position).findViewById(R.id.tv_text);
PopupMenu popupMenu = new PopupMenu(item.getContext(), item);
boolean findItemVisibility = message.getSenderId().equals(presenter.getAuthManager().getCurrentUserId())
&& position == messagesAdapter.getItemCount() - 1;
popupMenu.inflate(R.menu.menu_message_item);
popupMenu.getMenu().findItem(R.id.item_edit).setVisible(findItemVisibility);
popupMenu.setOnMenuItemClickListener(menuItem -> {
switch (menuItem.getItemId()) {
case R.id.item_delete:
presenter.deleteMessage(message);
return true;
case R.id.item_edit:
showEditMessageDialog(message);
return true;
default:
return false;
}
});
popupMenu.show();
}
private void showPopupMenu(View view, final StatusRes statusRes) {
PopupMenu popupMenu = new PopupMenu(mContext, view);
if (statusRes.isIs_self()) {
popupMenu.inflate(R.menu.menu_item_status_self);
} else {
popupMenu.inflate(R.menu.menu_item_status);
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_delete:
showDeleteStatusDailog(statusRes);
break;
case R.id.action_copy:
copyStatus(statusRes);
break;
case R.id.action_share:
break;
}
return false;
}
});
popupMenu.show();
}
private boolean showBlockSubmenu(View view) {
final Jid jid = conversation.getJid();
final boolean showReject = !conversation.isWithStranger() && conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
PopupMenu popupMenu = new PopupMenu(getActivity(), view);
popupMenu.inflate(R.menu.block);
popupMenu.getMenu().findItem(R.id.block_contact).setVisible(jid.getLocal() != null);
popupMenu.getMenu().findItem(R.id.reject).setVisible(showReject);
popupMenu.setOnMenuItemClickListener(menuItem -> {
Blockable blockable;
switch (menuItem.getItemId()) {
case R.id.reject:
activity.xmppConnectionService.stopPresenceUpdatesTo(conversation.getContact());
updateSnackBar(conversation);
return true;
case R.id.block_domain:
blockable = conversation.getAccount().getRoster().getContact(jid.getDomain());
break;
default:
blockable = conversation;
}
BlockContactDialog.show(activity, blockable);
return true;
});
popupMenu.show();
return true;
}
@SuppressLint("NewApi")
private void showPopupMenu(View v){
//the popupMenu didn't exist in android 2.3.3 and below, so we are checking to see what version of android
//this is running on and then running only code we can.
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) { //should kept this demo from force closing if run on the wrong API... I think...
PopupMenu popupM = new PopupMenu(this, v);
popupM.inflate(R.menu.popup);
popupM.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), item.toString(),Toast.LENGTH_LONG).show();
label1.append("\n you clicked "+item.toString());
return true;
}
});
popupM.show();
}
}
void showMenu(final ListView listView, final View anchorView) {
final PopupMenu menu = new PopupMenu(listView.getContext(), anchorView);
menu.inflate(R.menu.tile_menu);
menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.tile_dual:
dual = !dual;
break;
case R.id.tile_locked:
locked = !locked;
if (locked) {
secured = true;
lockedOnly = false;
}
break;
case R.id.tile_locked_only:
lockedOnly = !lockedOnly;
break;
case R.id.tile_secured:
secured = !secured;
break;
}
updateMenu(menu.getMenu());
listView.invalidateViews();
return true;
}
});
updateMenu(menu.getMenu());
menu.show();
}
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.inflate(getMenuRes());
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
public void onChangeColor(@NonNull View sender) {
ITagInterface itag = (ITagInterface) sender.getTag();
if (itag == null) {
ITagApplication.handleError(new Exception("No itag"));
return;
}
final PopupMenu popupMenu = new PopupMenu(this, sender);
popupMenu.inflate(R.menu.color);
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.black:
ITag.store.setColor(itag.id(), TagColor.black);
break;
case R.id.white:
ITag.store.setColor(itag.id(), TagColor.white);
break;
case R.id.red:
ITag.store.setColor(itag.id(), TagColor.red);
break;
case R.id.green:
ITag.store.setColor(itag.id(), TagColor.green);
break;
case R.id.gold:
ITag.store.setColor(itag.id(), TagColor.gold);
break;
case R.id.blue:
ITag.store.setColor(itag.id(), TagColor.blue);
break;
}
ITagApplication.faColorITag();
return true;
});
popupMenu.show();
}
@Override
public void onShowLongClicked(MediathekShow show, View view) {
this.longClickShow = show;
PopupMenu menu = new PopupMenu(getContext(), view, Gravity.TOP | Gravity.END);
menu.inflate(R.menu.activity_mediathek_detail);
menu.show();
menu.setOnMenuItemClickListener(this::onContextMenuItemClicked);
}
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.inflate(getMenuRes());
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
/**
* Create the popup menu for rotating the image.
*
* @param view The view opening the menu.
*/
private void showRotateMenu(final View view) {
PopupMenu popup = new PopupMenu(getActivity(), view);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(@NonNull final MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_rotate_right) {
mImageView.rotateImage((short) ExifInterface.ORIENTATION_ROTATE_90);
return true;
}
else if (itemId == R.id.action_rotate_left) {
mImageView.rotateImage((short) ExifInterface.ORIENTATION_ROTATE_270);
return true;
}
else if (itemId == R.id.action_rotate_180) {
mImageView.rotateImage((short) ExifInterface.ORIENTATION_ROTATE_180);
return true;
}
else {
return true;
}
}
});
popup.inflate(R.menu.menu_image_rotate);
popup.show();
}
protected void showPopupActioinMenu(View v) {
int position = getPositionForView(v);
L.d("postion %d at all %d", position, mAdapter.getCount());
Post post = (Post) mAdapter.getItem(position);
mMenuPost = post;
int links;
if (position == 0) {
mMenuTopicDetails = mData.mTopicDetails;
links = mData.getTopicLinksSize();
} else {
links = post.getLinksSize();
mMenuTopicDetails = null;
}
PopupMenu menu = new PopupMenu(getActivity(), v);
menu.inflate(R.menu.post_action_menu);
Menu m = menu.getMenu();
m.findItem(R.id.menu_post_flag).setVisible(post.showFlag() && false);// TODO 第一个版本 不支持该功能
MenuItem bookmark = m.findItem(R.id.menu_post_bookmark);
bookmark.setVisible(App.isLogin());
bookmark.setChecked(post.bookmarked);
MenuItem link = m.findItem(R.id.menu_post_links);
link.setVisible(links > 0);
link.setTitle(getResources().getString(R.string.menu_links, links));
MenuItem posters = m.findItem(R.id.menu_poster_count);
if (position == 0) {
posters.setVisible(false);// TODO 应该为true,第一个版本不支持该功能
int posterSize = mData.getPosterSize();
posters.setTitle(getResources().getString(R.string.menu_poster_count, posterSize));
} else {
posters.setVisible(false);
}
m.findItem(R.id.menu_post_delete).setVisible(post.can_delete);
menu.setOnMenuItemClickListener(mMenuListener);
menu.show();
}
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.inflate(getMenuRes());
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
public void onFilePopUpClicked(View v, int position) {
final PopupMenu menu = new PopupMenu(getActivity(), v);
menu.setOnMenuItemClickListener(item -> {
ArrayList<Song> songs = new ArrayList<>();
Collection<File> files = FileUtils.listFiles(new File(fileFolderPathList.get(position)), new String[]{"mp3", "ma4", "ogg", "wav"}, false);
for (File file : files) {
songs.add(getSongs(new String[]{file.getAbsolutePath()}));
}
if (songs.size() == 0) {
Toast.makeText(mContext, R.string.audio_files_not_found, Toast.LENGTH_SHORT).show();
return false;
}
switch (item.getItemId()) {
case R.id.popup_file_play:
mApp.getPlayBackStarter().playSongs(songs, 0);
startActivity(new Intent(mContext, NowPlayingActivity.class));
break;
case R.id.popup_file_add_to_queue:
new AsyncAddTo(getString(R.string.songs_added_to_queue), true, songs).execute();
break;
case R.id.popup_file_play_next:
new AsyncAddTo(getString(R.string.will_be_played_next), false, songs).execute();
break;
}
return false;
});
menu.inflate(R.menu.popup_file);
menu.show();
}
@OnClick(R.id.overflow)
public void onOverflowClicked(View v) {
PopupMenu popup = new PopupMenu(getActivity(), v);
popup.inflate(R.menu.video_overflow);
popup.setOnMenuItemClickListener(overflowMenuClickListener);
popup.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
setShareIntent();
//noinspection SimplifiableIfStatement
if (id == R.id.action_reminder) {
//open the schedule dialog
final Calendar c = Calendar.getInstance();
//fill the notificationCursor
notificationCursor = DbAccess.getNotificationByNoteId(getBaseContext(), this.id);
hasAlarm = notificationCursor.moveToFirst();
if (hasAlarm) {
notification_id = notificationCursor.getInt(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_ID));
}
if (hasAlarm) {
//ask whether to delete or update the current alarm
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.action_reminder));
popupMenu.inflate(R.menu.reminder);
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
} else {
//create a new one
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(TextNoteActivity.this, this, year, month, day);
dpd.getDatePicker().setMinDate(c.getTimeInMillis());
dpd.show();
}
return true;
} else if (id == R.id.action_save) {
if (ContextCompat.checkSelfPermission(TextNoteActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(TextNoteActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityCompat.requestPermissions(TextNoteActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_EXTERNAL_STORAGE);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(TextNoteActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_EXTERNAL_STORAGE);
}
} else {
saveToExternalStorage();
}
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Create the popup menu for saving metadata.
*
* @param view The view opening the menu.
*/
private void showSaveMenu(final View view) {
PopupMenu popup = new PopupMenu(getActivity(), view);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(@NonNull final MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_store_color_settings) {
mImageView.storeColorSettings(false);
return true;
}
else if (itemId == R.id.action_reset_color_settings) {
mImageView.storeColorSettings(true);
return true;
}
else if (itemId == R.id.action_store_position) {
mImageView.storePositionZoom(false);
return true;
}
else if (itemId == R.id.action_reset_position) {
mImageView.storePositionZoom(true);
return true;
}
else if (itemId == R.id.action_store_overlay_color) {
mImageView.storeOverlayColor(false);
return true;
}
else if (itemId == R.id.action_reset_overlay_color) {
mImageView.storeOverlayColor(true);
return true;
}
else if (itemId == R.id.action_delete_overlay_position) {
mImageView.resetOverlayPosition(true);
return true;
}
else {
return true;
}
}
});
popup.inflate(R.menu.menu_image_save);
if (mShowUtilities == UtilitiyStatus.SHOW_NOTHING) {
popup.getMenu().removeGroup(R.id.group_overlay);
}
else {
try {
JpegMetadata metadata = mImageView.getEyePhoto().getImageMetadata();
if (!metadata.hasOverlayPosition()) {
popup.getMenu().removeGroup(R.id.group_overlay);
}
else if (metadata.hasFlag(JpegMetadata.FLAG_OVERLAY_SET_BY_CAMERA_ACTIVITY)
|| metadata.hasFlag(JpegMetadata.FLAG_OVERLAY_POSITION_DETERMINED_AUTOMATICALLY)) {
popup.getMenu().removeItem(R.id.action_store_overlay_color);
popup.getMenu().removeItem(R.id.action_reset_overlay_color);
}
}
catch (NullPointerException e) {
// ignore
}
}
if (mShowUtilities == UtilitiyStatus.SHOW_NOTHING || mShowUtilities == UtilitiyStatus.ONLY_OVERLAY) {
popup.getMenu().removeGroup(R.id.group_color_settings);
}
popup.show();
}
public void onPopUpMenuClickListener(View v, int position) {
final PopupMenu menu = new PopupMenu(mContext, v);
SubMenu sub = (menu.getMenu()).addSubMenu(0, ADD_TO_PLAYLIST, 1, R.string.add_to_playlist);
MusicUtils.makePlaylistMenu(getContext(), sub, 0);
mPosition = position;
ArrayList<Song> songs = CursorHelper.getTracksForSelection("ARTIST", "" + mArtistList.get(position)._artistId);
if (checkIfAlbumsEmpty(songs, mPosition)) return;
menu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.popup_album_play_next:
new AsyncAddTo(mArtistList.get(position)._artistName, false, songs).execute();
return true;
case R.id.popup_album_add_to_queue:
new AsyncAddTo(mArtistList.get(position)._artistName, true, songs).execute();
return true;
case NEW_PLAYLIST:
PlaylistDialog playlistDialog = new PlaylistDialog();
Bundle bundle = new Bundle();
bundle.putLongArray("PLAYLIST_IDS", MusicUtils.getPlayListIds(songs));
playlistDialog.setArguments(bundle);
playlistDialog.show(getActivity().getSupportFragmentManager(), "FRAGMENT_TAG");
return true;
case PLAYLIST_SELECTED:
MusicUtils.insertIntoPlayList(mContext, item, songs);
return true;
case R.id.popup_album_delete:
try {
MusicUtils.deleteFile(FragmentArtist.this, songs, this);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
return true;
default:
break;
}
return false;
});
menu.inflate(R.menu.popup_album);
menu.show();
}
public void onPopUpMenuClickListener(View v, final int position) {
final PopupMenu menu = new PopupMenu(mContext, v);
SubMenu sub = (menu.getMenu()).addSubMenu(0, ADD_TO_PLAYLIST, 1, R.string.add_to_playlist);
MusicUtils.makePlaylistMenu(getContext(), sub, 0);
mPosition = position;
ArrayList<Song> songs = CursorHelper.getTracksForSelection("ALBUMS", "" + mAlbums.get(position)._Id);
if (checkIfAlbumsEmpty(songs, position)) return;
menu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.popup_album_play_next:
new AsyncAddTo(mAlbums.get(position)._albumName, false, songs).execute();
return true;
case R.id.popup_album_add_to_queue:
new AsyncAddTo(mAlbums.get(position)._albumName, true, songs).execute();
return true;
case NEW_PLAYLIST:
PlaylistDialog playlistDialog = new PlaylistDialog();
Bundle bundle = new Bundle();
bundle.putLongArray("PLAYLIST_IDS", MusicUtils.getPlayListIds(songs));
playlistDialog.setArguments(bundle);
playlistDialog.show(getActivity().getSupportFragmentManager(), "FRAGMENT_TAG");
return true;
case PLAYLIST_SELECTED:
MusicUtils.insertIntoPlayList(mContext, item, songs);
return true;
case R.id.popup_album_delete:
try {
MusicUtils.deleteFile(AlbumFragment.this, songs, AlbumFragment.this);
} catch (IndexOutOfBoundsException e) {
}
return true;
default:
break;
}
return false;
});
menu.inflate(R.menu.popup_album);
menu.show();
}
@Override
public void OnPopUpMenuClicked(View view, int position) {
mSelectedPosition = position;
PopupMenu menu = new PopupMenu(getActivity(), view);
SubMenu sub = (menu.getMenu()).addSubMenu(0, ADD_TO_PLAYLIST, 1, R.string.add_to_playlist);
MusicUtils.makePlaylistMenu(getContext(), sub, 0);
menu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.popup_song_play_next:
new AsyncAddTo(mSongList.get(position)._title, false, mSongList.get(position)).execute();
break;
case R.id.popup_song_addto_queue:
new AsyncAddTo(mSongList.get(position)._title, true, mSongList.get(position)).execute();
break;
case R.id.popup_song_add_to_favs:
mApp.getDBAccessHelper().addToFavorites(mSongList.get(position));
break;
case R.id.popup_song_delete:
ArrayList<Song> song = new ArrayList<>();
song.add(mSongList.get(mSelectedPosition));
try {
MusicUtils.deleteFile(SongsFragment.this, song, SongsFragment.this);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
break;
case R.id.popup_song_use_as_phone_ringtone:
MusicUtils.setRingtone((AppCompatActivity) getActivity(), mSongList.get(mSelectedPosition)._id);
break;
case R.id.popup_song_share:
MusicUtils.shareTheMusic(SongsFragment.this.getActivity(), mSongList.get(mSelectedPosition)._path);
break;
case R.id.popup_edit_songs_tags:
Intent intent = new Intent(getActivity(), Id3TagEditorActivity.class);
intent.putExtra("SONG_PATH", mSongList.get(mSelectedPosition)._path);
intent.putExtra("ALBUM_ID", mSongList.get(mSelectedPosition)._albumId);
startActivityForResult(intent, Constants.EDIT_TAGS);
break;
case NEW_PLAYLIST:
PlaylistDialog playlistDialog = new PlaylistDialog();
Bundle bundle = new Bundle();
bundle.putLongArray("PLAYLIST_IDS", new long[]{mSongList.get(mSelectedPosition)._id});
playlistDialog.setArguments(bundle);
playlistDialog.show(getActivity().getSupportFragmentManager(), "FRAGMENT_TAG");
return true;
case PLAYLIST_SELECTED:
long[] list = new long[]{mSongList.get(mSelectedPosition)._id};
long playlist = item.getIntent().getLongExtra("playlist", 0);
MusicUtils.addToPlaylist(getContext(), list, playlist);
return true;
}
return false;
});
menu.inflate(R.menu.popup_song);
menu.show();
}