【问题标题】:How to change background resource of an image button to implement on/off switch [duplicate]如何更改图像按钮的背景资源以实现开/关切换[重复]
【发布时间】:2013-03-18 12:20:44
【问题描述】:

我有两个电源按钮图像,一个是红色的,另一个是绿色的。我想创建一个按钮,最初将其背景资源设置为红色电源按钮。我希望它的资源在按下时变为绿色,再次单击后,我希望它再次变为红色。请帮忙...

【问题讨论】:

标签: java android android-layout


【解决方案1】:

如何使用自定义开/关图形制作切换按钮

使用带有自定义选择器的CheckBox

这将提供在启用和禁用状态的选中和未选中图像之间切换的能力,而无需对 Java 代码进行任何编程干预。

示例 - XML 布局:

<CheckBox
    android:id="@+id/my_custom_toggle"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:button="@drawable/my_selector"
/>

示例 - drawable/my_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="true"
    android:state_enabled="false"
    android:drawable="@drawable/ic_button_custom_toggle_disabled"
    />
<item
    android:state_checked="false"
    android:state_enabled="false"
    android:drawable="@drawable/ic_button_custom_toggle_disabled"
    />
<item
    android:state_checked="true"
    android:drawable="@drawable/ic_button_custom_toggle_linked"
    />
<item
    android:state_checked="false"
    android:drawable="@drawable/ic_button_custom_toggle_unlinked"
    />

为上述每个州添加自定义 .png 图像。

【讨论】:

    【解决方案2】:

    这样做:

    <ToggleButton 
            android:id="@+id/toggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/check"   //check.xml
            android:layout_margin="10dp"
            android:textOn=""
            android:textOff=""
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_centerVertical="true"/>
    

    在drawable文件夹中创建check.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- When selected, use grey -->
        <item android:drawable="@drawable/selected_image"
              android:state_checked="true" />
        <!-- When not selected, use white-->
        <item android:drawable="@drawable/unselected_image"
            android:state_checked="false"/>
    
     </selector>
    

    这很好用。

    【讨论】:

      【解决方案3】:

      您需要将两个红色和绿色的图像放在可绘制文件夹中。

      static int set = 0;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          final ImageButton toggle = (ImageButton) findViewById(R.id.imageButton1);
      
          toggle.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
      
                  if(set==0)
                  {
                      toggle.setBackgroundResource(R.drawable.red);
                      set=1;
                  }
                  else
                  {
                      toggle.setBackgroundResource(R.drawable.green);
                      set=0;
                  }
      
              }
          });
      }  
      

      【讨论】:

        【解决方案4】:

        使用ToggleButton。许多可用的示例,例如 here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 2013-09-07
          • 2016-07-12
          • 2011-03-05
          相关资源
          最近更新 更多