单击时更改我的ToggleButton的颜色

问题描述 投票:1回答:3

我有一些切换按钮代表一周中的日子。

当用户点击ToggleButton时,我希望它切换状态并更改颜色,向用户指示它已被点击。

这就是我的ToggleButtons之一:

<ToggleButton
    android:id="@+id/toggleButton_monday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/button_border"
    android:textOff="MON"
    android:textOn="MON"
    app:layout_constraintEnd_toStartOf="@+id/toggleButton_tuesday"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView10" />

enter image description here

我希望按钮在点击后看起来像这样(我想要相同的边框和形状,但只是改变了不同的颜色和状态):

enter image description here


答案here不起作用,因为我已经有一个android:background我的切换按钮显示按钮周围的自定义边框。

答案here不起作用,因为我使用ToggleButton而不是SwitchCompat


编辑:

这是当前的android:background我设置我的ToggleButtons:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="10dp"
        />
    <solid
        android:color="#FFFFFF"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="75dp"
        android:height="40dp"
        />
    <stroke
        android:width="3dp"
        android:color="#878787"
        />
</shape>

编辑:解决方案

谢谢@Broken和@Moustafa Shahin,我结合使用了他们的答案。

首先,我为我的ToggleButtons toggle_button_default.xmltoggle_button_checked.xml创建了两个资源文件(在第一个EDIT中检查上面的代码)。基本上,背景颜色在两个XML中是不同的。

其次,我创建了一个名为toggle_button_state.xml的seletor,并加载了我在上面创建的相应资源文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- WHEN IS CHECKED -->
    <item android:drawable="@drawable/toggle_button_default" android:state_checked="false" />

    <!-- WHEN IS NOT CHECKED -->
    <item android:drawable="@drawable/toggle_button_checked" android:state_checked="true" />

</selector>

最后,对于我的ToggleButtons,我将toggle_button_state设置为它们的背景:

<ToggleButton
    android:id="@+id/toggleButton_monday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/toggle_button_state"
    android:textOff="MON"
    android:textOn="MON"/>

在活动中,我有一个onClick方法,我可以用来监控点击:

class RentActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.toggleButton_monday ->{
                Toast.makeText(this, "Monday Clicked", Toast.LENGTH_LONG).show()
                return
            }
     ...
android android-layout togglebutton
3个回答
1
投票

您必须在drawable目录中创建具有状态列表的新文件。

你可以命名为toggle_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- WHEN IS CHECKED -->
    <item android:drawable="@color/colorPrimary" android:state_checked="false" />

    <!-- WHEN IS NOT CHECKED -->
    <item android:drawable="@color/colorAccent" android:state_checked="true" />

</selector>

上面的文件你可以用作backgroundToggleButton

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ToggleButton
        android:id="@+id/toggleButton_monday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/toggle_background"
        android:textOff="ON"
        android:textOn="OFF" />

</LinearLayout>

默认情况下按钮具有灰色边框。您想什么时候删除它只需添加:

style="?android:attr/borderlessButtonStyle"

到xml文件中的ToggleButton

你也可以添加OnCheckedChangeListener。如果你有许多按钮,你可以将它们全部添加到列表中,并在循环中为所有按钮添加相同的侦听器:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<ToggleButton> listOfButtons = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Add ToggleButtons to list
        listOfButtons.add(findViewById(R.id.toggleButton_monday));

        // Create listener for all of them
        CompoundButton.OnCheckedChangeListener listener = (buttonView, isChecked) -> {
            // Do something
        };


        // Add listener to all od buttons
        for (ToggleButton button : listOfButtons) {
            button.setOnCheckedChangeListener(listener);
        }
    }
}


1
投票

您可以创建2个后台资源文件并根据状态进行更改:

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        // The toggle is enabled
        //the background resource which mean status is enable
    } else {
        // The toggle is disabled
        //the background resource which mean status is disabled
    }
}

});


0
投票

如果您正在寻找一种优雅的方法来执行此操作,您可能想尝试使用GridView ....

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:columnWidth="100dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="24dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="spacingWidthUniform"
    />

...这将允许您使用项目填充布局,使您可以更轻松地迭代它们。您可以创建自定义适配器并将其分配给gridview:

GridView gridview = findViewById(R.id.gridview);
MyCustomAdapter _Adapter = new MyCustomAdapter(getApplicationContext());
gridview.setAdapter(_Adapter);

这将允许您在一个位置为所有按钮设置单个onClickListener:

gridview.setOnItemClickListener((parent, view, position, id) -> {
        _Adapter.toggleItem(position);
    });

您可以找到有关创建自定义gridview适配器here的深入教程

© www.soinside.com 2019 - 2024. All rights reserved.