下面列出了怎么用androidx.appcompat.widget.AppCompatRadioButton的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateRadioGroup(final Map<String, DeviceInfo> devices)
{
if (dialogList != null)
{
dialogList.removeAllViews();
for (DeviceInfo deviceInfo : devices.values())
{
deviceInfo.selected = false;
if (deviceInfo.responses > 0)
{
final ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.RadioButtonStyle);
final AppCompatRadioButton b = new AppCompatRadioButton(wrappedContext, null, 0);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(lp);
b.setText(deviceInfo.message.getDescription());
b.setTextColor(Utils.getThemeColorAttr(context, android.R.attr.textColor));
b.setOnClickListener(v ->
{
deviceInfo.selected = true;
stop();
});
dialogList.addView(b);
}
}
dialogList.invalidate();
}
}
private void onRadioBtnChange(final AppCompatRadioButton[] radioGroup, AppCompatRadioButton v)
{
for (AppCompatRadioButton r : radioGroup)
{
r.setChecked(false);
}
v.setChecked(true);
}
@NonNull
private AppCompatRadioButton createCompatRadioButton(
RadioGroup group, String contentDescription) {
AppCompatRadioButton button = new AppCompatRadioButton(getContext());
button.setContentDescription(contentDescription);
group.addView(button);
return button;
}
@Override
public void customizeRadioButton(AppCompatRadioButton button) {
button.setText(contentDescription);
LinearLayout.LayoutParams size =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
MarginLayoutParamsCompat.setMarginEnd(
size, button.getResources().getDimensionPixelSize(R.dimen.theme_switcher_radio_spacing));
button.setLayoutParams(size);
}
private void init() {
layoutSelectorView = activity.getLayoutInflater().inflate(R.layout.stream_layout_preview, null);
final RadioGroup rg = layoutSelectorView.findViewById(R.id.layouts_radiogroup);
final FrameLayout previewWrapper = layoutSelectorView.findViewById(R.id.preview_wrapper);
if (previewMaxHeightRes != -1) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
(int) activity.getResources().getDimension(previewMaxHeightRes)
);
previewWrapper.setLayoutParams(lp);
//previewWrapper.setMinimumHeight((int) activity.getResources().getDimension(previewMaxHeightRes));
}
ViewStub preview = layoutSelectorView.findViewById(R.id.layout_stub);
preview.setLayoutResource(previewLayout);
final View inflated = preview.inflate();
for (int i = 0; i < layoutTitles.length; i++) {
final String layoutTitle = layoutTitles[i];
final AppCompatRadioButton radioButton = new AppCompatRadioButton(activity);
radioButton.setText(layoutTitle);
final int finalI = i;
radioButton.setOnClickListener(v -> selectCallback.onSelected(layoutTitle, finalI, inflated));
if (textColor != -1) {
radioButton.setTextColor(Service.getColorAttribute(textColor, R.color.black_text, activity));
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.GRAY, //Disabled
Service.getColorAttribute(R.attr.colorAccent, R.color.accent, activity), //Enabled
}
);
radioButton.setSupportButtonTintList(colorStateList);
}
radioButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, // Width
(int) activity.getResources().getDimension(R.dimen.layout_selector_height) // Height
));
rg.addView(radioButton, i);
if ((selectedLayoutIndex != -1 && selectedLayoutIndex == i) || (selectedLayoutTitle != null && selectedLayoutTitle.equals(layoutTitle))) {
radioButton.performClick();
}
}
}
@SuppressLint("DefaultLocale")
private void editFavoriteConnection(@NonNull final MenuItem m, final BroadcastResponseMsg msg)
{
final FrameLayout frameView = new FrameLayout(activity);
activity.getLayoutInflater().inflate(R.layout.dialog_favorite_connect_layout, frameView);
final TextView deviceAddress = frameView.findViewById(R.id.device_address);
deviceAddress.setText(String.format("%s %s",
activity.getString(R.string.connect_dialog_address),
msg.getHostAndPort()));
// Connection alias
final EditText deviceAlias = frameView.findViewById(R.id.device_alias);
deviceAlias.setText(msg.getAlias());
// Optional identifier
final EditText deviceIdentifier = frameView.findViewById(R.id.device_identifier);
deviceIdentifier.setText(msg.getIdentifier());
final AppCompatRadioButton renameBtn = frameView.findViewById(R.id.device_rename_connection);
final AppCompatRadioButton deleteBtn = frameView.findViewById(R.id.device_delete_connection);
final AppCompatRadioButton[] radioGroup = {renameBtn, deleteBtn};
for (AppCompatRadioButton r : radioGroup)
{
r.setOnClickListener((View v) ->
{
if (v != renameBtn)
{
deviceAlias.clearFocus();
deviceIdentifier.clearFocus();
}
onRadioBtnChange(radioGroup, (AppCompatRadioButton)v);
});
}
deviceAlias.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus)
{
onRadioBtnChange(radioGroup, renameBtn);
}
});
deviceIdentifier.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus)
{
onRadioBtnChange(radioGroup, renameBtn);
}
});
final Drawable icon = Utils.getDrawable(activity, R.drawable.drawer_edit_item);
Utils.setDrawableColorAttr(activity, icon, android.R.attr.textColorSecondary);
final AlertDialog dialog = new AlertDialog.Builder(activity)
.setTitle(R.string.favorite_connection_edit)
.setIcon(icon)
.setCancelable(false)
.setView(frameView)
.setNegativeButton(activity.getResources().getString(R.string.action_cancel), (dialog1, which) ->
{
Utils.showSoftKeyboard(activity, deviceAlias, false);
dialog1.dismiss();
})
.setPositiveButton(activity.getResources().getString(R.string.action_ok), (dialog12, which) ->
{
Utils.showSoftKeyboard(activity, deviceAlias, false);
// rename or delete favorite connection
if (renameBtn.isChecked() && deviceAlias.getText().length() > 0)
{
final String alias = deviceAlias.getText().toString();
final String identifier = deviceIdentifier.getText().length() > 0 ?
deviceIdentifier.getText().toString() : null;
final BroadcastResponseMsg newMsg = configuration.favoriteConnections.updateDevice(
msg, alias, identifier);
updateItem(m, R.drawable.drawer_favorite_device, newMsg.getAlias(), () -> editFavoriteConnection(m, newMsg));
}
if (deleteBtn.isChecked())
{
configuration.favoriteConnections.deleteDevice(msg);
m.setVisible(false);
m.setChecked(false);
}
activity.getDeviceList().updateFavorites(false);
dialog12.dismiss();
}).create();
dialog.show();
Utils.fixIconColor(dialog, android.R.attr.textColorSecondary);
}
private void init() {
layoutSelectorView = activity.getLayoutInflater().inflate(R.layout.stream_layout_preview, null);
final RadioGroup rg = (RadioGroup) layoutSelectorView.findViewById(R.id.layouts_radiogroup);
final FrameLayout previewWrapper = (FrameLayout) layoutSelectorView.findViewById(R.id.preview_wrapper);
if (previewMaxHeightRes != -1) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
(int) activity.getResources().getDimension(previewMaxHeightRes)
);
previewWrapper.setLayoutParams(lp);
//previewWrapper.setMinimumHeight((int) activity.getResources().getDimension(previewMaxHeightRes));
}
ViewStub preview = (ViewStub) layoutSelectorView.findViewById(R.id.layout_stub);
preview.setLayoutResource(previewLayout);
final View inflated = preview.inflate();
for (int i = 0; i < layoutTitles.length; i++) {
final String layoutTitle = layoutTitles[i];
final AppCompatRadioButton radioButton = new AppCompatRadioButton(activity);
radioButton.setText(layoutTitle);
final int finalI = i;
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectCallback.onSelected(layoutTitle, finalI, inflated);
}
});
if (textColor != -1) {
radioButton.setTextColor(Service.getColorAttribute(textColor, R.color.black_text, activity));
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.GRAY, //Disabled
Service.getColorAttribute(R.attr.colorAccent, R.color.accent, activity), //Enabled
}
);
radioButton.setSupportButtonTintList(colorStateList);
}
radioButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, // Width
(int) activity.getResources().getDimension(R.dimen.layout_selector_height) // Height
));
rg.addView(radioButton, i);
if ((selectedLayoutIndex != -1 && selectedLayoutIndex == i) || (selectedLayoutTitle != null && selectedLayoutTitle.equals(layoutTitle))) {
radioButton.performClick();
}
}
}
@Override
public void onClick(View v) {
ScrollView scrollView = new ScrollView(getContext());
RadioGroup group = new RadioGroup(getContext());
int vPadding = DimenUtils.dpToPx(12);
group.setPadding(0, vPadding, 0, vPadding);
AppCompatRadioButton normalButton = (AppCompatRadioButton) LayoutInflater.from(getContext()).inflate(R.layout.item_dialog_radio_button, group, false);
normalButton.setId(0);
normalButton.setText(R.string.font_default);
normalButton.setChecked(preference == null || preference.length() == 0);
group.addView(normalButton);
for (int i = 0; i < items.size(); i++) {
String item = items.get(i);
AppCompatRadioButton button = (AppCompatRadioButton) LayoutInflater.from(getContext()).inflate(R.layout.item_dialog_radio_button, group, false);
button.setId(i + 1);
button.setText(item.replace(".ttf", ""));
button.setTag(item);
try {
button.setTypeface(Typeface.createFromAsset(getContext().getAssets(), item));
} catch (Exception e) {
continue;
}
button.setChecked(preference != null && preference.equals(item));
group.addView(button);
}
group.setOnCheckedChangeListener((group1, checkedId) -> {
for (int i = 0; i < group1.getChildCount(); i++) {
RadioButton child = (RadioButton) group1.getChildAt(i);
child.setChecked(child.getId() == checkedId);
if (child.getId() == checkedId)
selectedPreference = (String) child.getTag();
}
});
scrollView.addView(group);
new AlertDialog.Builder(getContext())
.setTitle(getIdentifier().getTitle())
.setView(scrollView)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
FontPreferenceData.this.preference = selectedPreference;
getIdentifier().setPreferenceValue(getContext(), selectedPreference);
onPreferenceChange(selectedPreference);
selectedPreference = null;
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> selectedPreference = null)
.show();
}
private void initializeThemingValues(
RadioGroup group,
@ArrayRes int overlays,
@ArrayRes int contentDescriptions,
@StyleableRes int[] themeOverlayAttrs,
@IdRes int overlayId,
ThemingType themingType) {
TypedArray themingValues = getResources().obtainTypedArray(overlays);
TypedArray contentDescriptionArray = getResources().obtainTypedArray(contentDescriptions);
if (themingValues.length() != contentDescriptionArray.length()) {
throw new IllegalArgumentException(
"Feature array length doesn't match its content description array length.");
}
for (int i = 0; i < themingValues.length(); i++) {
@StyleRes int valueThemeOverlay = themingValues.getResourceId(i, 0);
ThemeAttributeValues themeAttributeValues = null;
// Create RadioButtons for themeAttributeValues values
switch (themingType) {
case COLOR:
themeAttributeValues = new ColorPalette(valueThemeOverlay, themeOverlayAttrs);
break;
case SHAPE_CORNER_FAMILY:
themeAttributeValues = new ThemeAttributeValues(valueThemeOverlay);
break;
case SHAPE_CORNER_SIZE:
themeAttributeValues =
new ThemeAttributeValuesWithContentDescription(
valueThemeOverlay, contentDescriptionArray.getString(i));
break;
}
// Expect the radio group to have a RadioButton as child for each themeAttributeValues value.
AppCompatRadioButton button =
themingType.radioButtonType == RadioButtonType.XML
? ((AppCompatRadioButton) group.getChildAt(i))
: createCompatRadioButton(group, contentDescriptionArray.getString(i));
button.setTag(themeAttributeValues);
themeAttributeValues.customizeRadioButton(button);
int currentThemeOverlay = ThemeOverlayUtils.getThemeOverlay(overlayId);
if (themeAttributeValues.themeOverlay == currentThemeOverlay) {
group.check(button.getId());
}
}
themingValues.recycle();
contentDescriptionArray.recycle();
}
@Override
public void customizeRadioButton(AppCompatRadioButton button) {
CompoundButtonCompat.setButtonTintList(
button, ColorStateList.valueOf(convertToDisplay(main)));
}
@NonNull
@Override
protected AppCompatRadioButton createRadioButton(Context context, AttributeSet attrs) {
return new MaterialRadioButton(context, attrs);
}
public void customizeRadioButton(AppCompatRadioButton button) {}