如何实现在材料设计指南中规定的切换按钮在这里?
是否可以从Android设计支持库中获得开箱即用,或者是否有可用的第三方库?
我有十个切换按钮.我想在单击主页按钮时保存其中五个按钮的状态.但我想保存它只有当用户对任何按钮的状态进行了更改时.有没有办法在不使用setOnClickListener()的情况下知道状态的变化?
无法将 MaterialToggleButton 的选定颜色设置为纯色,仅显示原色的透明阴影。
我尝试了下面的代码集,输出图像如下所示。
样式中的按钮主题
<style name="ThemeButtonColorToggle" parent="AppTheme">
<item name="colorPrimary">@color/colorOrange</item>
<item name="colorButtonNormal">@color/colorOrange</item>
<item name="android:backgroundTint">@color/black</item>
<item name="strokeColor">@color/colorOrange</item>
<item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
<item name="colorOnPrimary">@color/colorOrange</item>
<item name="colorOnPrimarySurface">@color/colorOrange</item>
</style>
Run Code Online (Sandbox Code Playgroud)
XML代码
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedButton="@id/btnAndroid"
app:layout_constraintTop_toBottomOf="@id/tvName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:theme="@style/ThemeButtonColorToggle"
android:textColor="@android:color/white"
android:textColorHighlight="@color/colorOrange"
android:text="Android" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btniOS"
android:layout_width="wrap_content"
android:textIsSelectable="true"
android:textColor="@android:color/white"
android:theme="@style/ThemeButtonColorToggle"
android:layout_height="wrap_content"
android:text="iOS" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
我需要得到如下所示的纯色
谁能帮我解决一下
android android-button android-togglebutton material-components-android
是否有选项可以MaterialButtonToggleGroup在使用时选择所需的按钮app:singleSelection="true"?
当单击不同的按钮工作正常(所有其他按钮都被取消选择),但是当您单击同一个(已选择)按钮时,它会自行取消选择它,我想保持选中状态。
我的例子:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/filterA"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterB"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterC"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud) android android-button android-togglebutton material-components material-components-android
我想MaterialButtonToggleGroup与 2一起使用MaterialButton。我想在里面以相同的宽度显示它们MaterialButtonToggleGroup
如果我使用,match_parent那么只会显示一个按钮,如果我使用wrap_content它们,它们会显示在中间。
下面是当前的输出

以下是我的代码:-
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center"
app:checkedButton="@id/btn_login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_login"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_register"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
</com.google.android.material.button.MaterialButtonToggleGroup>```
Please help. Thanks
Run Code Online (Sandbox Code Playgroud) 我试图在我的项目中放置一个按钮切换组,其行为类似于单选按钮组,但看起来不像单选按钮组(即,当选择一个按钮时,将取消选择其他按钮)。
我遵循了在网上找到的单选按钮模式,但这似乎不起作用。有没有办法做到这一点?我已经到了这样的地步:按钮位于我想要的位置,但它们都被禁用了。
MovieSpotterTheme() {
Card(
modifier = Modifier
.fillMaxWidth()
) {
@Composable
fun MaterialButtonToggleGroup() {
var selected by remember { mutableStateOf("Android") }
val buttonGroup = listOf("Popular Movies", "Search Movies")
val onSelectedChange = { text: String ->
selected = text
}
Row(
horizontalArrangement = Arrangement.SpaceEvenly
) {
buttonGroup.forEach { text ->
Row(Modifier
.selectable(
selected = (text == selected),
onClick = { onSelectedChange(text) }
)
.padding(horizontal = 16.dp)
) {
Button(
enabled = (text == selected),
onClick = { onSelectedChange(text) }
) …Run Code Online (Sandbox Code Playgroud) 我有这样的方法:
ToggleButton toggle = ((ToggleButton)findViewById(R.id.toggle));
toggle.setTextOn("blah");
toggle.setTextOff("blahblah");
toggle.invalidate(); // doesn't work?
Run Code Online (Sandbox Code Playgroud)
调用此方法onOptionsItemSelected.切换按钮LinearLayout位于另一个内部LinearLayout.
我希望在调用方法后立即更新文本.相反,切换后的文本仅在我手动按下以切换状态后才更新.我在这里错过了什么,我使用了错误的方法吗?为什么不起作用.invalidate?
我想ToggleButton在Android studio中添加一个设计模式.
我的目标是API级别28 targetSdkVersion 28
我的ToggleButton没有显示,我看到了这个错误:
java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed
这是xmlToggleButton的内容
<ToggleButton
android:text="ToggleButton"
android:layout_width="100dp"
android:layout_height="100dp" tools:layout_editor_absoluteY="369dp"
tools:layout_editor_absoluteX="133dp" android:id="@+id/toggleButton5" android:textOff="OFF"
android:textOn="ON" android:textColor="@color/colorAccent" style="@style/AlertDialog.AppCompat"
tools:text="Hello" android:typeface="normal" android:textStyle="bold" android:textAlignment="center"
/>
Run Code Online (Sandbox Code Playgroud)
显然在API 26中不推荐使用clipRect(RectF rect,Region.Op op),但我不确定需要更改什么?
这是完整的堆栈跟踪:
java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed
at android.graphics.Canvas.checkValidClipOp(Canvas.java:779)
at android.graphics.Canvas.clipRect(Canvas.java:826)
at android.view.ViewGroup_Delegate.drawShadow(ViewGroup_Delegate.java:86)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:58)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
at android.support.constraint.ConstraintLayout.dispatchDraw(ConstraintLayout.java:2023)
at android.view.View.draw_Original(View.java:20075)
at android.view.View_Delegate.draw(View_Delegate.java:68)
at android.view.View.draw(View.java:19849)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:4333)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:63)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333) …Run Code Online (Sandbox Code Playgroud) 我有一个ToggleButton.我希望背景清晰,就像一周中几天的默认闹钟应用程序一样.下面的代码涵盖了清晰颜色的切换.有没有办法保持切换和更改背景颜色而不滚动我自己的切换按钮?如果没有,整个过程都会非常糟糕,imo.另外,我真的必须在这里定义一个清晰的颜色,还是我可以在我的xml中使用内置的清晰颜色?
<ToggleButton
android:background="@drawable/clear_toggle_button"
android:id="@+id/btn_sunday"
android:layout_width="50dp"
android:layout_height="50dp"
android:textOn="SUN"
android:textOff="SUN"
/>
Run Code Online (Sandbox Code Playgroud)
这是我的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="clear">#ffffff00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我在drawable文件夹中的颜色状态列表xml .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/clear" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/clear" />
</selector>
Run Code Online (Sandbox Code Playgroud) android android-layout statelistdrawable android-togglebutton
我有一个EditText,我在那里听取文本的变化:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
// do stuff
}
});
Run Code Online (Sandbox Code Playgroud)
到目前为止这个工作正常,如果我在EditText中输入内容,就会执行afterTextChanged()中的内容.现在,在同一个活动中,我有一个ToggleButton,它可以改变EditText中的字符串.由于ToggleButton触发"afterTextChanged",如何防止此文本更改?
PS:不确定这是否相关,但具体来说我有一个接受小数或小数的EditText(例如"0.75"或"3/4"),切换按钮应在小数和小数显示之间切换,但不应触发任何内容在"afterTextChanged"中,因为值保持不变(3/4 = 0.75).
android ×10
material-components-android ×2
button ×1
invalidation ×1
refresh ×1
textwatcher ×1
view ×1