Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 多状态切换按钮_Android_User Interface_Button_Togglebutton - Fatal编程技术网

Android 多状态切换按钮

Android 多状态切换按钮,android,user-interface,button,togglebutton,Android,User Interface,Button,Togglebutton,在我一直在开发的应用程序中,我希望有一个多状态(在我的例子中是三个)切换按钮,而不是ToggleButton提供的两个。我试着开始自己的扩展按钮,遵循复合按钮源代码,但老实说,阅读它的源代码有点让人不知所措 有没有一种方法可以只使用选择器xml或其他什么,或者使用我没有想到的另一种方法来实现三态切换按钮?我不知道该怎么做。您当然可以定义一个选择器,将其用作有三个条目的背景。问题是您可以为选择器使用哪些按钮属性。您可以有两个布尔属性,例如A和B,并根据A、B和默认值定义选择器。(A&&B将满足A,

在我一直在开发的应用程序中,我希望有一个多状态(在我的例子中是三个)切换按钮,而不是
ToggleButton
提供的两个。我试着开始自己的扩展
按钮
,遵循
复合按钮
源代码,但老实说,阅读它的源代码有点让人不知所措


有没有一种方法可以只使用选择器xml或其他什么,或者使用我没有想到的另一种方法来实现三态切换按钮?我不知道该怎么做。

您当然可以定义一个选择器,将其用作有三个条目的背景。问题是您可以为选择器使用哪些按钮属性。您可以有两个布尔属性,例如A和B,并根据A、B和默认值定义选择器。(A&&B将满足A,因此更恰当地说,它们可以被视为A、!A&&B和!A&&B。)您可以重载现有属性(选择、聚焦等),或者更优雅地,使用所述配方定义您自己的自定义属性。

我实现了一个多状态切换按钮,源代码是

这就是它的样子:

而且它很容易使用:

<org.honorato.multistatetogglebutton.MultiStateToggleButton
    android:id="@+id/mstb_multi_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    mstb:values="@array/planets_array" />

您可以创建一个自定义的ImageButton来实现这一点,在这种情况下,您需要3个不同的图像。 如果需要,还可以添加更多状态

public class FlashButton extends ImageButton {

    public enum FlashEnum {
        AUTOMATIC, ON, OFF
    }

    public interface FlashListener {
        void onAutomatic();
        void onOn();
        void onOff();
    }

    private FlashEnum mState;
    private FlashListener mFlashListener;

    public FlashButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        //Sets initial state
        setState(FlashEnum.AUTOMATIC);
    }


    @Override
    public boolean performClick() {
        super.performClick();
        int next = ((mState.ordinal() + 1) % FlashEnum.values().length);
        setState(FlashEnum.values()[next]);
        performFlashClick();
        return true;
    }


    private void performFlashClick() {
        if(mFlashListener == null)return;
        switch (mState) {
            case AUTOMATIC:
                mFlashListener.onAutomatic();
                break;
            case ON:
                mFlashListener.onOn();
                break;
            case OFF:
                mFlashListener.onOff();
                break;
        }
    }

    private void createDrawableState() {
        switch (mState) {
            case AUTOMATIC:
                setImageResource(R.drawable.ic_flash_auto);
                break;
            case ON:
                setImageResource(R.drawable.ic_flash_on);
                break;
            case OFF:
                setImageResource(R.drawable.ic_flash_off);
                break;
        }
    }


    public FlashEnum getState() {
        return mState;
    }

    public void setState(FlashEnum state) {
        if(state == null)return;
        this.mState = state;
        createDrawableState();

    }

    public FlashListener getFlashListener() {
        return mFlashListener;
    }

    public void setFlashListener(FlashListener flashListener) {
        this.mFlashListener = flashListener;
    }

}

为什么不在内部使用
RadioGroup
和样式收音机

 <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/your_drawable_selector"
        android:button="@android:color/transparent"
        android:gravity="center_horizontal" //center text
        android:text="text"
         />
...

...

芯片是非常好的本机选择

 <com.google.android.material.chip.ChipGroup
      android:id="@+id/chip_group"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:singleSelection="true">

      <com.google.android.material.chip.Chip
           android:id="@+id/first_chip"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="Todo"
           android:textAppearance="?android:attr/textAppearance"
           android:textColor="@color/black"
           android:checkable="true"
           style="@style/Widget.MaterialComponents.Chip.Choice"
           app:chipBackgroundColor="@color/colorAccent"/>

           <!-- Second Chip -->
           <!-- Third Chip -->

   </com.google.android.material.chip.ChipGroup>
德国劳埃德船级社


查看您链接的线程:我理解第1步,但第2步我有一些问题。在创建新视图时,是否最好扩展视图、按钮或其他内容?除了构造函数和onCreateDrawableState()之外,还有其他方法需要重载吗?如果这些都是基本的,我很抱歉,这是我第一个真正的应用。没关系,让它工作吧!谢谢你向正确的方向努力,我很感激。@moonfire是你的专有代码吗?我也在尝试创建一个三态切换按钮,希望看到一些其他的解决方案来让我思考如何去做。@johnmeta很抱歉回答得太晚,我应该经常在这里查看。如果你还需要帮助,我只是在上发了一个帖子。或者,如果您只想下载示例源代码:。希望有帮助!你让人们变得懒惰;)你是如何在图片中定制的?在您的文档中,您没有提到任何保管。这是一个旧版本,请尝试搜索旧的提交。但基本上您必须修改背景绘图内容。@jlhonora感谢您提供了这段伟大的代码!我想知道是否有可能在切换按钮之间进行转换,而不是直接从一个按钮跳到另一个按钮,是否有方法在它们之间进行转换?@jlhonora如果我将值
Yes
从活动a传递到切换按钮所在的活动B,我如何使用选中的值
Yes
设置切换按钮的状态?
 <com.google.android.material.chip.ChipGroup
      android:id="@+id/chip_group"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:singleSelection="true">

      <com.google.android.material.chip.Chip
           android:id="@+id/first_chip"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="Todo"
           android:textAppearance="?android:attr/textAppearance"
           android:textColor="@color/black"
           android:checkable="true"
           style="@style/Widget.MaterialComponents.Chip.Choice"
           app:chipBackgroundColor="@color/colorAccent"/>

           <!-- Second Chip -->
           <!-- Third Chip -->

   </com.google.android.material.chip.ChipGroup>
binding.chipGroup.setOnCheckedChangeListener { chipGroup, i -> 
    when (i) {
        binding.firstChip -> {
            binding.firstChip.setChipBackgroundColorResource(R.color.colorAccent)
        }
        else -> {} 
    }
}
binding.firstChip.isChecked = true  //default