下面列出了怎么用androidx.appcompat.widget.AppCompatButton的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateMultiroomChannelBtn(AppCompatButton b, @Nullable final State state)
{
MultiroomDeviceInformationMsg.ChannelType ch = state != null ?
state.multiroomChannel : MultiroomDeviceInformationMsg.ChannelType.NONE;
if (ch != MultiroomDeviceInformationMsg.ChannelType.NONE && isGroupMenu())
{
final MultiroomChannelSettingMsg cmd = new MultiroomChannelSettingMsg(
state.getActiveZone() + 1, MultiroomChannelSettingMsg.getUpType(ch));
b.setVisibility(View.VISIBLE);
b.setText(ch.toString());
setButtonEnabled(b, state.isOn());
final Drawable icon = Utils.getDrawable(activity, R.drawable.cmd_multiroom_channel);
Utils.setDrawableColorAttr(activity, icon,
state.isOn() ? R.attr.colorButtonEnabled : R.attr.colorButtonDisabled);
b.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
prepareButtonListeners(b, cmd, null);
}
else
{
b.setVisibility(View.GONE);
setButtonEnabled(b, false);
b.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}
@Override
public void onMasterVolumeChange(int progressChanged)
{
// This callback is called when master volume slider is changed.
// We shall update the text of the "Audio control" button
if (!activity.isConnected())
{
return;
}
final State state = activity.getStateManager().getState();
for (View view : deviceSoundButtons)
{
if (view instanceof AppCompatButton && audioControlManager.isVolumeLevel(view))
{
final AppCompatButton b = (AppCompatButton) view;
final String vol = State.getVolumeLevelStr(progressChanged, state.getActiveZoneInfo());
b.setText(vol);
}
}
}
private AppCompatButton createButton(final AlertDialog alertDialog,
final Document document, final Element button,
final CustomPopupMsg.UiType uiType)
{
final AppCompatButton b = new AppCompatButton(context);
b.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
b.setGravity(Gravity.CENTER);
b.setText(button.getAttribute("text"));
b.setOnClickListener(v ->
{
Utils.showSoftKeyboard(context, v, false);
button.setAttribute("selected", "true");
alertDialog.dismiss();
final CustomPopupMsg outMsg = new CustomPopupMsg(uiType, documentToXml(document));
buttonListener.onButtonSelected(outMsg);
});
return b;
}
AppCompatButton createButton(@StringRes int descriptionId,
final ISCPMessage msg, Object tag, final ButtonListener listener)
{
ContextThemeWrapper wrappedContext = new ContextThemeWrapper(activity, R.style.TextButtonStyle);
final AppCompatButton b = new AppCompatButton(wrappedContext, null, 0);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, buttonSize);
lp.setMargins(buttonMarginHorizontal, 0, buttonMarginHorizontal, 0);
b.setLayoutParams(lp);
b.setText(descriptionId);
b.setTag(tag);
prepareButtonListeners(b, msg, listener);
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 init(Context context) {
inflate(context, R.layout.post_filter_view, this);
activeBoard = (AppCompatSpinner) findViewById(R.id.active_board);
activeBoard.setOnItemSelectedListener(onBoardClicked());
nameInput = (AppCompatEditText) findViewById(R.id.filter_name);
filterInput = (AppCompatEditText) findViewById(R.id.filter_text);
highlightCheckBox = (AppCompatCheckBox) findViewById(R.id.highlight_checkbox);
saveButton = (AppCompatButton) findViewById(R.id.save_button);
saveButton.setOnClickListener(this);
findViewById(R.id.edit_button).setOnClickListener(this);
findViewById(R.id.cancel_button).setOnClickListener(this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
AppCompatButton button = new AppCompatButton(inflater.getContext());
button.setText(R.string.optimizations_name);
button.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED) {
// This intent launches the "evil dialog of misleading and restrictive user freedom", according
// to Google Play's policy, but there shouldn't actually be anything wrong with it. The policy
// states that this is only acceptable if battery optimization affects the "core functionality" of
// the app in question, which... it does. However, it seems their moderators have decided otherwise,
// as the update that I pushed containing this intent was taken down. Which is why the permission to use
// this intent is now only granted in the OSS product flavor. :(
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getContext().getApplicationContext().getPackageName()));
startActivityForResult(intent, REQUEST_OPTIMIZATION);
} else {
startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), REQUEST_OPTIMIZATION);
Toast.makeText(getContext(), R.string.msg_battery_optimizations_switch_enable, Toast.LENGTH_LONG).show();
}
});
return button;
}
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 prepareDeviceSoundButtons(final State.SoundControlType soundControl)
{
clearSoundVolumeButtons();
soundControlLayout.setVisibility(View.VISIBLE);
// Here, we create zone-dependent buttons without command message.
// The message for active zone will be assigned in updateActiveView
if (soundControl == State.SoundControlType.DEVICE_SLIDER &&
soundControlLayout.getTag() != null && soundControlLayout.getTag().equals("portrait"))
{
audioControlManager.createSliderSoundControl(this, soundControlLayout);
}
else
{
audioControlManager.createButtonsSoundControl(this, soundControlLayout);
}
for (int i = 0; i < soundControlLayout.getChildCount(); i++)
{
deviceSoundButtons.add(soundControlLayout.getChildAt(i));
}
// fast selector for listening mode
listeningModeLayout.setVisibility(View.VISIBLE);
if (listeningModeLayout.getChildCount() == 1)
{
final LinearLayout l = (LinearLayout) listeningModeLayout.getChildAt(0);
l.removeAllViews();
for (ListeningModeMsg.Mode m : activity.getConfiguration().audioControl.getSortedListeningModes(true, null))
{
final ListeningModeMsg msg = new ListeningModeMsg(m);
final AppCompatButton b = createButton(
msg.getMode().getDescriptionId(), msg, msg.getMode(), null);
l.addView(b);
b.setVisibility(View.GONE);
deviceSoundButtons.add(b);
}
}
}
private void updatePageMode(int pageMode) {
for (int i = 0; i < pageAnimModeView.getChildCount(); i++) {
View child = pageAnimModeView.getChildAt(i);
if (child instanceof AppCompatButton) {
int pos = Integer.parseInt((String) child.getTag());
child.setSelected(pageMode == pos);
}
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
AppCompatButton button = new AppCompatButton(inflater.getContext());
button.setText(R.string.action_access_grant);
button.setOnClickListener(v -> {
startActivityForResult(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), REQUEST_ACCESSIBILITY);
Toast.makeText(getContext(), R.string.msg_notification_switch_enable, Toast.LENGTH_LONG).show();
});
return button;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
AppCompatButton button = new AppCompatButton(inflater.getContext());
button.setText(R.string.action_access_grant);
button.setOnClickListener(v -> startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getActivity().getPackageName())), REQUEST_OVERLAY));
return button;
}
static void setButtonColor(
AppCompatButton button,
int buttonColor,
int buttonTextColor,
int buttonPressedColor,
int buttonPressedTextColor) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{}
};
ColorStateList buttonColours = new ColorStateList(
states,
new int[]{
buttonPressedColor,
buttonPressedColor,
buttonColor
});
ColorStateList buttonTextColours = new ColorStateList(
states,
new int[]{
buttonPressedTextColor,
buttonPressedTextColor,
buttonTextColor
});
ViewCompat.setBackgroundTintList(button, buttonColours);
button.setTextColor(buttonTextColours);
}
@NonNull
@Override
protected AppCompatButton createButton(@NonNull Context context, @NonNull AttributeSet attrs) {
return new MaterialButton(context, attrs);
}