下面列出了怎么用androidx.appcompat.widget.AppCompatImageButton的API类实例代码及写法,或者点击链接到github查看源代码。
private void prepareAmplifierButtons()
{
clearSoundVolumeButtons();
soundControlLayout.setVisibility(View.VISIBLE);
listeningModeLayout.setVisibility(View.GONE);
final AmpOperationCommandMsg.Command[] commands = new AmpOperationCommandMsg.Command[]
{
AmpOperationCommandMsg.Command.AMTTG,
AmpOperationCommandMsg.Command.MVLDOWN,
AmpOperationCommandMsg.Command.MVLUP
};
for (AmpOperationCommandMsg.Command c : commands)
{
final AmpOperationCommandMsg msg = new AmpOperationCommandMsg(c.getCode());
final AppCompatImageButton b = createButton(
msg.getCommand().getImageId(), msg.getCommand().getDescriptionId(),
msg, msg.getCommand().getCode());
soundControlLayout.addView(b);
amplifierButtons.add(b);
}
}
AppCompatImageButton createButton(
@DrawableRes int imageId, @StringRes int descriptionId,
final ISCPMessage msg, Object tag,
int leftMargin, int rightMargin, int verticalMargin)
{
ContextThemeWrapper wrappedContext = new ContextThemeWrapper(activity, R.style.ImageButtonPrimaryStyle);
final AppCompatImageButton b = new AppCompatImageButton(wrappedContext, null, 0);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(buttonSize, buttonSize);
lp.setMargins(leftMargin, verticalMargin, rightMargin, verticalMargin);
b.setLayoutParams(lp);
b.setTag(tag);
prepareButton(b, msg, imageId, descriptionId);
return b;
}
static void collectButtons(LinearLayout layout, ArrayList<View> out)
{
for (int k = 0; k < layout.getChildCount(); k++)
{
View v = layout.getChildAt(k);
if (v instanceof AppCompatImageButton || v instanceof AppCompatButton)
{
out.add(v);
}
else if (v instanceof TextView && v.getTag() != null)
{
out.add(v);
}
if (v instanceof LinearLayout)
{
collectButtons((LinearLayout) v, out);
}
}
}
private void updateItem(@NonNull final MenuItem m, final @DrawableRes int iconId, final String title, final ButtonListener editListener)
{
if (m.getActionView() != null && m.getActionView() instanceof LinearLayout)
{
final LinearLayout l = (LinearLayout)m.getActionView();
((AppCompatImageView) l.findViewWithTag("ICON")).setImageResource(iconId);
((AppCompatTextView)l.findViewWithTag("TEXT")).setText(title);
final AppCompatImageButton editBtn = l.findViewWithTag("EDIT");
if (editListener != null)
{
editBtn.setVisibility(View.VISIBLE);
editBtn.setOnClickListener(v -> editListener.onEditItem());
Utils.setButtonEnabled(activity, editBtn, true);
}
else
{
editBtn.setVisibility(View.GONE);
}
}
m.setVisible(true);
}
@Override
public void onClick(View b)
{
if (b instanceof AppCompatImageButton)
{
if (b.getId() == R.id.palette_settings_button)
{
DialogPaletteSettings d = new DialogPaletteSettings(context, this, visibleGroups);
d.show();
}
else if (listChangeIf != null)
{
final PaletteButton pb = (PaletteButton) b;
listChangeIf.onPalettePressed(pb.getCode());
}
}
}
private void prepareGroup(Context context, LinearLayout itemLayout, String s, List<String> visibleGroups)
{
final AppCompatCheckBox cb = itemLayout.findViewById(R.id.dialog_palette_settings_checkbox);
cb.setTag(s);
cb.setChecked(visibleGroups.contains(s));
final LinearLayout buttonLayout = itemLayout.findViewById(R.id.dialog_palette_settings_buttons);
for (int i = 0; i < buttonLayout.getChildCount(); i++)
{
if (buttonLayout.getChildAt(i) instanceof AppCompatImageButton)
{
final AppCompatImageButton b = (AppCompatImageButton) buttonLayout.getChildAt(i);
b.setOnLongClickListener(this);
ViewUtils.setImageButtonColorAttr(context, b, R.attr.colorDialogContent);
}
}
}
/**
* Verify the Mini Controller is displayed
* Click the Mini Controller and verify the Expanded Controller is displayed
*/
@Test
public void testMiniController() throws InterruptedException, UiObjectNotFoundException {
mTestUtils.connectToCastDevice();
mTestUtils.playCastContent(VIDEO_TITLE);
// click to close expanded controller
onView(allOf(
isAssignableFrom(AppCompatImageButton.class)
, withParent(isAssignableFrom(Toolbar.class))
))
.check(matches(isDisplayed()))
.perform(click());
mTestUtils.verifyMiniController();
mDevice.pressEnter();
onView(withId(R.id.cast_mini_controller))
.perform(click());
mTestUtils.verifyExpandedController();
mTestUtils.disconnectFromCastDevice();
}
private void initWidget() {
AppCompatImageButton backBtn = findViewById(R.id.activity_introduce_backBtn);
backBtn.setOnClickListener(v -> finishSelf(true));
if (introduceModelList.size() <= 1) {
findViewById(R.id.activity_introduce_buttonBar).setVisibility(View.GONE);
}
setBottomButtonStyle(0);
initPage();
InkPageIndicator indicator = findViewById(R.id.activity_introduce_indicator);
if (introduceModelList.size() <= 1) {
indicator.setAlpha(0f);
} else {
indicator.setViewPager(viewPager);
indicator.setAlpha(1f);
}
}
/**
* Procedure sets AppCompatImageButton color given by attribute ID
*/
private static void setImageButtonColorAttr(Context context, AppCompatImageButton b, @AttrRes int resId)
{
final int c = getThemeColorAttr(context, resId);
b.clearColorFilter();
b.setColorFilter(c, PorterDuff.Mode.SRC_ATOP);
}
public static void setButtonEnabled(Context context, View b, boolean isEnabled)
{
@AttrRes int resId = isEnabled ? R.attr.colorButtonEnabled : R.attr.colorButtonDisabled;
b.setEnabled(isEnabled);
if (b instanceof AppCompatImageButton)
{
Utils.setImageButtonColorAttr(context, (AppCompatImageButton) b, resId);
}
if (b instanceof AppCompatButton)
{
((AppCompatButton) b).setTextColor(Utils.getThemeColorAttr(context, resId));
}
}
public static void setButtonSelected(Context context, View b, boolean isSelected)
{
@AttrRes int resId = isSelected ? R.attr.colorAccent : R.attr.colorButtonEnabled;
b.setSelected(isSelected);
if (b instanceof AppCompatImageButton)
{
Utils.setImageButtonColorAttr(context, (AppCompatImageButton) b, resId);
}
if (b instanceof AppCompatButton)
{
((AppCompatButton) b).setTextColor(Utils.getThemeColorAttr(context, resId));
}
}
private void prepareFmDabButtons()
{
for (AppCompatImageButton b : fmDabButtons)
{
String[] tokens = b.getTag().toString().split(":");
if (tokens.length != 2)
{
continue;
}
final String msgName = tokens[0];
switch (msgName)
{
case PresetCommandMsg.CODE:
final PresetCommandMsg pMsg = new PresetCommandMsg(
ReceiverInformationMsg.DEFAULT_ACTIVE_ZONE, tokens[1]);
if (pMsg.getCommand() != null)
{
prepareButton(b, pMsg, pMsg.getCommand().getImageId(), pMsg.getCommand().getDescriptionId());
}
break;
case TuningCommandMsg.CODE:
final TuningCommandMsg tMsg = new TuningCommandMsg(
ReceiverInformationMsg.DEFAULT_ACTIVE_ZONE, tokens[1]);
if (tMsg.getCommand() != null)
{
prepareButton(b, tMsg, tMsg.getCommand().getImageId(), tMsg.getCommand().getDescriptionId());
}
break;
}
}
}
private void setButtonsEnabled(List<AppCompatImageButton> buttons, boolean flag)
{
for (AppCompatImageButton b : buttons)
{
setButtonEnabled(b, flag);
}
}
private void setButtonsVisibility(List<AppCompatImageButton> buttons, int flag)
{
for (AppCompatImageButton b : buttons)
{
b.setVisibility(flag);
}
}
private void updateInputSource(@DrawableRes int imageId, final boolean visible)
{
AppCompatImageButton btn = rootView.findViewById(R.id.btn_input_selector);
btn.setImageResource(imageId);
btn.setVisibility(visible ? View.VISIBLE : View.GONE);
setButtonEnabled(btn, false);
}
private void updateMultiroomGroupBtn(AppCompatImageButton b, @Nullable final State state)
{
if (state != null && isGroupMenu())
{
final boolean isMaster = state.getMultiroomRole() == MultiroomDeviceInformationMsg.RoleType.SRC;
b.setVisibility(View.VISIBLE);
setButtonEnabled(b, true);
setButtonSelected(b, isMaster);
b.setContentDescription(activity.getString(R.string.cmd_multiroom_group));
prepareButtonListeners(b, null, () ->
{
if (activity.isConnected())
{
final AlertDialog alertDialog = MultiroomManager.createDeviceSelectionDialog(
activity, b.getContentDescription());
alertDialog.show();
Utils.fixIconColor(alertDialog, android.R.attr.textColorSecondary);
}
});
}
else
{
b.setVisibility(View.GONE);
setButtonEnabled(b, false);
}
}
private void updateFeedButton(final AppCompatImageButton btn, final MenuStatusMsg.Feed feed)
{
btn.setVisibility(feed.isImageValid() ? View.VISIBLE : View.GONE);
if (feed.isImageValid())
{
btn.setImageResource(feed.getImageId());
setButtonEnabled(btn, true);
setButtonSelected(btn, feed == MenuStatusMsg.Feed.LOVE);
}
}
AppCompatImageButton createButton(
@DrawableRes int imageId, @StringRes int descriptionId,
final ISCPMessage msg, Object tag)
{
return createButton(imageId, descriptionId, msg, tag,
buttonMarginHorizontal, buttonMarginHorizontal, buttonMarginVertical);
}
void prepareButton(@NonNull AppCompatImageButton b,
@DrawableRes final int imageId,
@StringRes final int descriptionId)
{
b.setImageResource(imageId);
if (descriptionId != -1)
{
b.setContentDescription(activity.getResources().getString(descriptionId));
}
}
void prepareButton(@NonNull AppCompatImageButton b,
final ISCPMessage msg,
@DrawableRes final int imageId,
@StringRes final int descriptionId)
{
prepareButton(b, imageId, descriptionId);
b.setImageResource(imageId);
prepareButtonListeners(b, msg);
setButtonEnabled(b, false);
}
@Override
public void setNavigationIcon(@Nullable Drawable icon) {
super.setNavigationIcon(icon);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child instanceof AppCompatImageButton) {
AppCompatImageButton button = (AppCompatImageButton) child;
button.setBackgroundResource(R.drawable.nav_toolbar_button_item_background);
}
}
}
}
@Override
public void onCreateView(View view) {
AppCompatImageButton refresh = view.findViewById(R.id.frequency_refresh);
AppCompatImageButton reset = view.findViewById(R.id.frequency_reset);
AppCompatImageButton restore = view.findViewById(R.id.frequency_restore);
refresh.setOnClickListener(v -> {
rotate(v, false);
if (mRefreshListener != null) {
mRefreshListener.onClick(v);
}
});
reset.setOnClickListener(v -> {
rotate(v, true);
if (mResetListener != null) {
mResetListener.onClick(v);
}
});
restore.setOnClickListener(v -> {
rotate(v, true);
if (mRestoreListener != null) {
mRestoreListener.onClick(v);
}
});
setFullSpan(true);
super.onCreateView(view);
}
@Override
public void onCreateView(View view) {
AppCompatImageButton refresh = view.findViewById(R.id.frequency_refresh);
AppCompatImageButton reset = view.findViewById(R.id.frequency_reset);
AppCompatImageButton restore = view.findViewById(R.id.frequency_restore);
refresh.setOnClickListener(v -> {
rotate(v, false);
if (mRefreshListener != null) {
mRefreshListener.onClick(v);
}
});
reset.setOnClickListener(v -> {
rotate(v, true);
if (mResetListener != null) {
mResetListener.onClick(v);
}
});
restore.setOnClickListener(v -> {
rotate(v, true);
if (mRestoreListener != null) {
mRestoreListener.onClick(v);
}
});
setFullSpan(true);
super.onCreateView(view);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if (child instanceof AppCompatImageButton) {
mThemeComponent.addColorProperty(R.attr.actionBarTextColorPrimary, (c) ->
ImageViewCompat.setImageTintList((ImageView) child, ColorStateList.valueOf(c)));
} else if (child instanceof ActionMenuView) {
mThemeComponent.addColorProperty(R.attr.actionBarTextColorPrimary, (c) -> {
ActionMenuView ch = (ActionMenuView) child;
Drawable d = DrawableCompat.wrap(ch.getOverflowIcon()).mutate();
DrawableCompat.setTint(d, c);
ch.setOverflowIcon(d);
});
}
}
@Override
public boolean onLongClick(View b)
{
if (b instanceof AppCompatImageButton)
{
return ViewUtils.showButtonDescription(context, b);
}
return false;
}
static void setImageButtonEnabled(boolean enabled, AppCompatImageButton item, /*int iconResId,*/ Context context) {
item.setEnabled(enabled);
//Drawable originalIcon = ContextCompat.getDrawable(context, iconResId);
//Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon);
//item.setImageDrawable(icon);
if (enabled)
item.setColorFilter(null);
else
item.setColorFilter(context.getColor(R.color.activityDisabledTextColor), PorterDuff.Mode.SRC_IN);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
AppCompatImageButton addButton = view.findViewById(R.id.applications_pref_dlg_add);
TooltipCompat.setTooltipText(addButton, getString(R.string.applications_pref_dlg_add_button_tooltip));
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
applicationsListView = view.findViewById(R.id.applications_pref_dlg_listview);
//applicationsListView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
applicationsListView.setLayoutManager(layoutManager);
applicationsListView.setHasFixedSize(true);
linlaProgress = view.findViewById(R.id.applications_pref_dlg_linla_progress);
rellaDialog = view.findViewById(R.id.applications_pref_dlg_rella_dialog);
listAdapter = new ApplicationsDialogPreferenceAdapterX(prefContext, preference, this);
// added touch helper for drag and drop items
ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(listAdapter, false, false);
itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(applicationsListView);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preference.startEditor(null);
}
});
refreshListView(false);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updatechannel);
mCardView = findViewById(R.id.updatechannel_card);
AppCompatImageButton mBack = findViewById(R.id.back_button);
mBack.setOnClickListener(v -> onBackPressed());
AppCompatImageButton mSave = findViewById(R.id.save_button);
mSave.setOnClickListener(v -> {
if (Utils.checkWriteStoragePermission(this)) {
if (mKernelNameHint.getText() != null && !mKernelNameHint.getText().toString().equals("")
&& mKernelVersionHint.getText() != null && !mKernelVersionHint.getText().toString().equals("")
&& mDownloadLinkHint.getText() != null && !mDownloadLinkHint.getText().toString().equals("")) {
saveUpdateChannel();
} else {
Utils.snackbar(mCardView, getString(R.string.update_channel_create_abort));
}
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
Utils.snackbar(mCardView, getString(R.string.permission_denied_write_storage));
}
});
AppCompatTextView mClearAll = findViewById(R.id.clear_all);
mClearAll.setOnClickListener(v -> {
if (isTextEntered()) {
new Dialog(this)
.setMessage(getString(R.string.clear_all_summary) + " " + getString(R.string.sure_question))
.setNegativeButton(getString(R.string.cancel), (dialog1, id1) -> {
})
.setPositiveButton(getString(R.string.yes), (dialog1, id1) -> {
clearAll();
})
.show();
} else {
Utils.snackbar(mCardView, getString(R.string.clear_all_abort_message));
}
});
mKernelNameHint = findViewById(R.id.kernel_name_hint);
mKernelVersionHint = findViewById(R.id.kernel_version_hint);
mDownloadLinkHint = findViewById(R.id.download_link_hint);
mChangelogHint = findViewById(R.id.changelog_hint);
mSHA1Hint = findViewById(R.id.sha1_hint);
mSupportHint = findViewById(R.id.support_hint);
mDonationHint = findViewById(R.id.donation_link_hint);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
initializeFragment(inflater, container, R.layout.device_fragment);
// Friendly name
{
friendlyName = rootView.findViewById(R.id.device_edit_friendly_name);
final AppCompatImageButton b = rootView.findViewById(R.id.device_change_friendly_name);
prepareButtonListeners(b, null, () ->
{
if (activity.isConnected())
{
activity.getStateManager().sendMessage(
new FriendlyNameMsg(friendlyName.getText().toString()));
// #119: Improve clearing focus for friendly name edit field
friendlyName.clearFocus();
}
});
setButtonEnabled(b, true);
// #119: Improve clearing focus for friendly name edit field
// OnClick for background layout: clear focus for friendly name text field
{
LinearLayout l = rootView.findViewById(R.id.device_background_layout);
l.setClickable(true);
l.setEnabled(true);
l.setOnClickListener((v) -> friendlyName.clearFocus());
}
}
prepareImageButton(R.id.btn_firmware_update, new FirmwareUpdateMsg(FirmwareUpdateMsg.Status.NET));
prepareImageButton(R.id.device_dimmer_level_toggle, new DimmerLevelMsg(DimmerLevelMsg.Level.TOGGLE));
prepareImageButton(R.id.device_digital_filter_toggle, new DigitalFilterMsg(DigitalFilterMsg.Filter.TOGGLE));
prepareImageButton(R.id.music_optimizer_toggle, new MusicOptimizerMsg(MusicOptimizerMsg.Status.TOGGLE));
prepareImageButton(R.id.device_auto_power_toggle, new AutoPowerMsg(AutoPowerMsg.Status.TOGGLE));
prepareImageButton(R.id.hdmi_cec_toggle, new HdmiCecMsg(HdmiCecMsg.Status.TOGGLE));
prepareImageButton(R.id.phase_matching_bass_toggle, new PhaseMatchingBassMsg(PhaseMatchingBassMsg.Status.TOGGLE));
prepareImageButton(R.id.sleep_time_toggle, null);
prepareImageButton(R.id.speaker_ab_command_toggle, null);
prepareImageButton(R.id.google_cast_analytics_toggle, null);
updateContent();
return rootView;
}
private void updateDeviceInformation(@Nullable final State state)
{
// Host
((TextView) rootView.findViewById(R.id.device_info_address)).setText(
(state != null) ? state.getHostAndPort() : "");
// Friendly name
final boolean isFnValid = state != null
&& state.isFriendlyName();
final LinearLayout fnLayout = rootView.findViewById(R.id.device_friendly_name);
fnLayout.setVisibility(isFnValid ? View.VISIBLE : View.GONE);
if (isFnValid)
{
// Friendly name
friendlyName.setText(state.getDeviceName(true));
}
// Receiver information
final boolean isRiValid = state != null
&& state.isReceiverInformation()
&& !state.deviceProperties.isEmpty();
final LinearLayout riLayout = rootView.findViewById(R.id.device_receiver_information);
riLayout.setVisibility(isRiValid ? View.VISIBLE : View.GONE);
if (isRiValid)
{
// Common properties
((TextView) rootView.findViewById(R.id.device_brand)).setText(state.deviceProperties.get("brand"));
((TextView) rootView.findViewById(R.id.device_model)).setText(state.getModel());
((TextView) rootView.findViewById(R.id.device_year)).setText(state.deviceProperties.get("year"));
// Firmware version
{
StringBuilder version = new StringBuilder();
version.append(state.deviceProperties.get("firmwareversion"));
if (state.firmwareStatus != FirmwareUpdateMsg.Status.NONE)
{
version.append(", ").append(getStringValue(state.firmwareStatus.getDescriptionId()));
}
((TextView) rootView.findViewById(R.id.device_firmware)).setText(version.toString());
}
// Update button
{
final AppCompatImageButton b = rootView.findViewById(R.id.btn_firmware_update);
b.setVisibility((state.firmwareStatus == FirmwareUpdateMsg.Status.NEW_VERSION ||
state.firmwareStatus == FirmwareUpdateMsg.Status.NEW_VERSION_FORCE) ?
View.VISIBLE : View.GONE);
if (b.getVisibility() == View.VISIBLE)
{
setButtonEnabled(b, true);
}
}
// Google cast version
((TextView) rootView.findViewById(R.id.google_cast_version)).setText(state.googleCastVersion);
}
final int[] deviceInfoLayout = new int[]{
R.id.device_info_layout,
R.id.device_info_divider
};
for (int layoutId : deviceInfoLayout)
{
rootView.findViewById(layoutId).setVisibility(isFnValid || isRiValid ? View.VISIBLE : View.GONE);
}
}