下面列出了android.view.MenuItem#setIcon ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_follow:
if ((boolean) SharedPreferencesUtils.get(App.getContext(), Integer.toString(id), false)) {
SharedPreferencesUtils.put(App.getContext(), Integer.toString(id), false);
Toast.makeText(getContext(), "已取消关注", Toast.LENGTH_SHORT).show();
item.setIcon(R.drawable.ic_add_follow);
} else {
SharedPreferencesUtils.put(App.getContext(), Integer.toString(id), true);
Toast.makeText(getContext(), "已关注", Toast.LENGTH_SHORT).show();
item.setIcon(R.drawable.ic_remove_follow);
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem undo = menu.findItem(R.id.undo_menu);
MenuItem redo = menu.findItem(R.id.redo_menu);
Drawable iconUndo = ContextCompat.getDrawable(activity, R.drawable.ic_undo_white_36dp);
Drawable iconRedo = ContextCompat.getDrawable(activity, R.drawable.ic_redo_white_36dp);
if (mCanvas.getLinesCount() == 0) {
iconUndo.setColorFilter(ContextCompat.getColor(activity, R.color.theme_default_primary_light),
PorterDuff.Mode.SRC_IN);
} else {
iconUndo.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
}
if (mCanvas.getUndoneLinesCount() == 0) {
iconRedo.setColorFilter(ContextCompat.getColor(activity, R.color.theme_default_primary_light),
PorterDuff.Mode.SRC_IN);
} else {
iconRedo.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
}
undo.setIcon(iconUndo);
redo.setIcon(iconRedo);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
for (ActionItem item : UiManager.getInstance().getMainActionBarItems()) {
MenuItem menuItem = menu.add(item.getGroupId(),
item.getId(),
item.getOrder(),
item.getTitle());
menuItem.setIcon(item.getIcon());
menuItem.setShowAsAction(item.getAction());
menuItem.setIntent(new Intent(this, item.getClazz()));
}
return super.onCreateOptionsMenu(menu);
}
/**
* Updates the bookmark item's visibility.
*
* @param bookmarkMenuItem {@link MenuItem} for adding/editing the bookmark.
* @param currentTab Current tab being displayed.
*/
protected void updateBookmarkMenuItem(MenuItem bookmarkMenuItem, Tab currentTab) {
bookmarkMenuItem.setEnabled(mBookmarkBridge.isEditBookmarksEnabled());
if (currentTab.getBookmarkId() != Tab.INVALID_BOOKMARK_ID) {
bookmarkMenuItem.setIcon(R.drawable.btn_star_filled);
bookmarkMenuItem.setChecked(true);
bookmarkMenuItem.setTitleCondensed(mActivity.getString(R.string.edit_bookmark));
} else {
bookmarkMenuItem.setIcon(R.drawable.btn_star);
bookmarkMenuItem.setChecked(false);
bookmarkMenuItem.setTitleCondensed(null);
}
}
@Override
public void changeNoteRvLayoutManagerAndMenuIcon(MenuItem item) {
if (Constans.noteListShowMode == NoteListConstans.STYLE_LINEAR) {
CacheManager.setAndSaveNoteListShowMode(NoteListConstans.STYLE_GRID);
mView.changeNoteRvLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
item.setIcon(MainApplication.mContext.getResources().getDrawable(NoteListConstans.MODE_LIST));
} else {
CacheManager.setAndSaveNoteListShowMode(NoteListConstans.STYLE_LINEAR);
mView.changeNoteRvLayoutManager(new LinearLayoutManager(MainApplication.mContext));
item.setIcon(MainApplication.mContext.getResources().getDrawable(NoteListConstans.MODE_GRID));
}
}
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Place an action bar item for searching.
MenuItem item = menu.add("Search");
item.setIcon(android.R.drawable.ic_menu_search);
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS
| MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
View searchView = SearchViewCompat.newSearchView(getActivity());
if (searchView != null) {
SearchViewCompat.setOnQueryTextListener(searchView,
new OnQueryTextListenerCompat() {
@Override
public boolean onQueryTextChange(String newText) {
// Called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query
// with this filter.
String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
// Don't do anything if the filter hasn't actually changed.
// Prevents restarting the loader when restoring state.
if (mCurFilter == null && newFilter == null) {
return true;
}
if (mCurFilter != null && mCurFilter.equals(newFilter)) {
return true;
}
mCurFilter = newFilter;
getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this);
return true;
}
});
MenuItemCompat.setActionView(item, searchView);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean show = Utils.isShowSystemApps(this);
MenuItem showItem = menu.findItem(R.id.show_system_apps);
if (showItem != null) {
showItem.setTitle(show ? R.string.hide_system_apps : R.string.show_system_apps);
showItem.setIcon(show ? R.drawable.ic_action_show : R.drawable.ic_action_hide);
}
MenuItem themeItem = menu.findItem(R.id.switch_theme);
if (themeItem != null) {
themeItem.setTitle(App.theme.title);
}
return super.onPrepareOptionsMenu(menu);
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_share) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
// i.putExtra(Intent.EXTRA_SUBJECT, "hack.chat");
String text = "Hack Chat! Join me for a chat on:\n" +
"https://hack.chat/?" + channel + "\n";
i.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(i, "Share with:"));
return true;
} else if (id == R.id.action_reconnect) {
getNickname(true);
return true;
} else if (id == R.id.action_notifications) {
if (item.getTitle().equals("Mute notifications")) {
allowNotifications = false;
item.setTitle("Allow notifications");
item.setIcon(R.mipmap.ic_notifications_off);
} else {
allowNotifications = true;
item.setTitle("Mute notifications");
item.setIcon(R.mipmap.ic_notifications_on);
}
} else if (id == R.id.action_json) {
sendRawJSON();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ImageBean image = mImages.get(mImagePager.getCurrentItem());
if (image.isSeleted()) {
if (!mChoseImageListener.onCancelSelect(image)) {
return true;
}
item.setIcon(R.drawable.image_check_off);
if (!all) {
mImages.remove(image);
if (mImages.size() <= 0) {
this.getFragmentManager().popBackStack();
return true;
}
mImagePager.removeAllViews();
mAdapter.notifyDataSetChanged();
}
} else {
if (!mChoseImageListener.onSelected(image)) {
return true;
}
item.setIcon(R.drawable.image_check_on);
}
return true;
}
/**
* Updates the bookmark item's visibility.
*
* @param bookmarkMenuItem {@link MenuItem} for adding/editing the bookmark.
* @param currentTab Current tab being displayed.
*/
protected void updateBookmarkMenuItem(MenuItem bookmarkMenuItem, Tab currentTab) {
bookmarkMenuItem.setEnabled(mBookmarkBridge.isEditBookmarksEnabled());
if (currentTab.getBookmarkId() != Tab.INVALID_BOOKMARK_ID) {
bookmarkMenuItem.setIcon(R.drawable.btn_star_filled);
bookmarkMenuItem.setChecked(true);
bookmarkMenuItem.setTitleCondensed(mActivity.getString(R.string.edit_bookmark));
} else {
bookmarkMenuItem.setIcon(R.drawable.btn_star);
bookmarkMenuItem.setChecked(false);
bookmarkMenuItem.setTitleCondensed(null);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem help = menu.add(
Menu.NONE, R.id.menu_id_targeted_help, Menu.NONE, R.string.menu_help);
help.setIcon(R.drawable.ic_help_and_feedback);
}
private void setNotebookPermissionIcon(Context context, Notebook book, MenuItem newNotebookEntry){
if(book.isShared()){
SharedNotebook shared = (SharedNotebook) book;
if(shared.isNoteCreationAllowed() && !shared.isNoteModificationAllowed()){
newNotebookEntry.setIcon(context.getResources().getDrawable(R.drawable.ic_note_add_black_24dp));
}else if(!shared.isNoteCreationAllowed() && !shared.isNoteModificationAllowed()){
newNotebookEntry.setIcon(context.getResources().getDrawable(R.drawable.ic_lock_black_24dp));
}else if(!shared.isNoteCreationAllowed() && shared.isNoteModificationAllowed()){
newNotebookEntry.setIcon(context.getResources().getDrawable(R.drawable.ic_create_black_24dp));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (infoTextId != 0) {
MenuItem item = menu.add(Menu.NONE, R.id.menu_info, Menu.NONE, R.string.menu_info);
item.setIcon(R.drawable.ic_info_outline_white_24dp);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return true;
}
@Test
public void sett_on_MenuItem() {
Drawable[] drawables = {new TestDrawable(), new TestDrawable()};
LayerDrawable layer = new LayerDrawable(drawables);
MenuItem menuItem = new TestMenuItem();
menuItem.setIcon(layer);
TestBadgeDrawable badge = Badger.sett(menuItem, factory);
assertBadged(menuItem.getIcon(), badge, drawables);
}
void initializeShareButton(Menu OptionsMenu) {
// Add share button
MenuItem shareItem = OptionsMenu.add(Menu.NONE, Menu.NONE, Menu.NONE, getString(R.string.share));
// Set up the view
shareItem.setIcon(R.drawable.ic_share);
// Specify the show flags
MenuItemCompat.setShowAsAction(shareItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
// On click, open share
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Prepare share intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
// Set as text/plain
shareIntent.setType("text/plain");
// Add text
shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.shareMessage));
// Show chooser
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareDesc)));
// Consume event
return true;
}
});
}
private void checkBookmarkMenuItem(MenuItem bookmarks) {
bookmarks.setChecked(true);
bookmarks.setIcon(R.drawable.ic_bookmark_white);
}
private void uncheckBookmarkMenuItem(MenuItem bookmarks) {
bookmarks.setChecked(false);
bookmarks.setIcon(R.drawable.ic_bookmark_outline_white);
}
protected void setOptionIcon(int id, int iconRes) {
MenuItem item = menu.findItem(id);
item.setIcon(iconRes);
}
protected void addBookmarkMenuItem(final Menu menu, final Bookmark b) {
final MenuItem bmi = menu.add(R.id.actions_goToBookmarkGroup, R.id.actions_goToBookmark, Menu.NONE, b.name);
bmi.setIcon(R.drawable.viewer_menu_bookmark);
ActionMenuHelper.setMenuItemExtra(bmi, "bookmark", b);
}
/**
* Function on select menu item
*
* @param item Menu item you select
* @return Flag or selection
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//switch id to do action
switch (id) {
//return to quran read
case R.id.action_read_quran:
Aya aya = new DatabaseAccess().getAyaFromPosition(6235 - translationViewPager.getCurrentItem());
startActivity(new Intent(this, QuranPageReadActivity.class)
.putExtra(AppConstants.General.PAGE_NUMBER, 604 - aya.pageNumber)); //position where return
AppPreference.setTranslationTextSize("large");
finish();
break;
//zoom in web_view
case R.id.action_zoom_in:
String size = AppPreference.getTranslationTextSize();
switch (size){
case "large":
AppPreference.setTranslationTextSize("x-large");
break ;
case "x-large":
AppPreference.setTranslationTextSize("xx-large");
item.setIcon(R.drawable.ic_zoom_r);
break ;
case "xx-large":
AppPreference.setTranslationTextSize("large");
item.setIcon(R.drawable.ic_zoom_in);
break ;
}
int previousPage = translationViewPager.getCurrentItem() ;
selectionTranslationsAdapter = null ;
selectionTranslationsAdapter = new SelectionTranslationsAdapter(getSupportFragmentManager(),
AppPreference.getDefaultTafseer() == -1 ? firstBook : AppPreference.getDefaultTafseer());
translationViewPager.setAdapter(selectionTranslationsAdapter);
translationViewPager.setCurrentItem(previousPage);
break ;
}
return super.onOptionsItemSelected(item);
}