【问题标题】:How to set image button backgroundimage for different state?如何为不同状态设置图像按钮背景图像?
【发布时间】:2011-01-21 06:02:22
【问题描述】:

我想要具有两种状态的图像按钮 i) 正常 ii) 触摸(或单击)。

我在 imagebutton 背景中设置了 normal 图片,我正在尝试更改 image(pressed)onclick 方法,但它不会改变。

我希望如果我按下图像按钮,那么图像应该从正常变为按下,直到我按下其他按钮但它不会发生

谁能建议我如何使用选择器或在运行时做到这一点?

这是我的图像按钮面板代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:gravity="bottom"
  android:id="@+id/buttonpanel">
  <ImageButton android:id="@+id/buttonhome"
               android:layout_width="80dp"
               android:layout_height="36dp"
               android:focusable="true" 
               android:background="@drawable/homeselector">
               </ImageButton>
  <ImageButton android:id="@+id/buttonsearch"
               android:layout_height="36dp"
               android:layout_width="80dp"
               android:background="@drawable/searchselector"
               android:focusable="true">
               </ImageButton>>
  <ImageButton android:id="@+id/buttonreg"
               android:layout_height="36dp"
               android:layout_width="80dp"
               android:background="@drawable/registerselector"
               android:focusable="true">
               </ImageButton>>
  <ImageButton android:id="@+id/buttonlogin"
               android:layout_height="36dp"
               android:layout_width="80dp"
               android:background="@drawable/loginselector"
               android:focusable="true">
               </ImageButton>
</LinearLayout>

我的选择器 xml 是

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--<item android:state_pressed="true" android:drawable="@drawable/homehover" 
        /> <item android:state_focused="true" android:drawable="@drawable/homehover" 
        /> <item android:drawable="@drawable/home" /> <item android:state_window_focused="true" 
        android:drawable="@drawable/homehover" /> -->
    <item android:state_enabled="false" android:drawable="@drawable/homehover" />
    <item android:state_pressed="true" android:state_enabled="true"
        android:drawable="@drawable/homehover" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/homehover" />
    <item android:state_enabled="true" android:drawable="@drawable/home" />

</selector> 

我也尝试在 ontouch 和 onclick 事件上更改图像资源,但没有帮助。

【问题讨论】:

  • 我认为您正在寻找类似this 的东西。我以前问过类似的问题。

标签: android android-layout


【解决方案1】:

像这样在你的drawable中创建一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/btn_sendemail_disable" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/btn_send_email_click" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/btn_sendemail_roll" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/btn_sendemail" />
</selector>

并相应地设置图像,然后将此 xml 设置为您的 imageButton 的背景。

【讨论】:

  • 正确。这将被称为 Selector XMLs
  • @Hitendra:我猜操作人员已经知道这一点。这个问题可以解释为为什么选择器没有被调用。
  • state_enabled 有必要吗?
  • 这适用于通常的Button,但不适用于ImageButton。这也适用于ImageButtonandroid:src 属性,但不适用于android:background。您的选择器和 OP 的选择器有什么区别(格式和可绘制对象的名称除外)?我错过了什么吗?
  • @AlexanderMalakhov 对我来说,它适用于 ImageButton 和 android:background。
【解决方案2】:

试试这个

btn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        btn.setBackgroundResource(R.drawable.icon);
    }
});

【讨论】:

  • 有没有人认为我可以做到
【解决方案3】:

嗯。我得到了另一个对我有帮助的解决方案,因为我只有两种不同的状态:

  • 该项目没有被点击
  • 或者它被点击,所以它会被高亮显示

在我的 .xml 中,我这样定义 ImageButton:

 <ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/selector" />

对应的选择器文件如下所示:

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

    <item android:drawable="@drawable/ico_100_checked" android:state_selected="true"/>
    <item android:drawable="@drawable/ico_100_unchecked"/>

</selector>

在我的 onCreate 中我调用:

final ImageButton ib = (ImageButton) value.findViewById(R.id.imageButton);

    OnClickListener ocl =new OnClickListener() {
        @Override
        public void onClick(View button) {
            if (button.isSelected()){
                button.setSelected(false);
            } else {
                ib.setSelected(false);
                //put all the other buttons you might want to disable here...
                button.setSelected(true);
            }
        }
    };

ib.setOnClickListener(ocl);
//add ocl to all the other buttons

完成。希望这可以帮助。我花了一段时间才弄清楚。

【讨论】:

    【解决方案4】:

    您好,试试下面的代码,它会对您有用,

    ((ImageView)findViewById(R.id.ImageViewButton)).setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
                ((ImageView) v.findViewById(R.id.ImageViewButton)).setImageResource(R.drawable.image_over);
    
            if(event.getAction() == MotionEvent.ACTION_UP)
                ((ImageView) v.findViewById(R.id.ImageViewButton)).setImageResource(R.drawable.image_normal);
    
            return false;
        }
    });
    

    【讨论】:

    • 实际上,如果 action_up 发生在不同的视图上,这也不起作用。尝试使用 event.getAction() == MotionEvent.ACTION_CANCEL 而不是 ACTION_UP。
    【解决方案5】:

    我认为你的问题不是选择器文件。

    你必须添加

    <imagebutton ..
        android:clickable="true"
    />
    

    到您的图像按钮。

    默认情况下,onClick 在列表项级别(父级)处理。并且 imageButtons 没有收到 onClick。

    当您添加上述属性时,图像按钮将接收事件并使用选择器。

    检查此POST,它对复选框的解释相同。

    【讨论】:

      【解决方案6】:

      如果你想按下图像按钮,那么图像应该从正常变为按下

      但我最好的方法是自定义 RadioButton 并在一个组中使用它们。我已经看到了一个例子。 抱歉,我不记得那个链接了。

      但如果你想避免这种情况。 您需要将此添加到您的 selector.xml

      一旦完成。刚刚得到你的代码并添加这个

      public void onClick ( View v ) {
          myImageButton.setSelected ( true ) ;
       }
      

      您将看到结果。但是您必须管理最近按下按钮的状态。这样你就可以设置

        myOLDImageButton.setSelected ( false ) ;
      

      我建议你将所有按钮引用放在一个数组中。

      【讨论】:

        【解决方案7】:

        您尝试做的更多是分段按钮而不是图像按钮列表。

        这里的http://blog.bookworm.at/2010/10/segmented-controls-in-android.html 是一个关于如何做到这一点的例子。基本思想是自定义 RadioButton 而不是 ImageButton,因为 RadioButton 将具有您需要的选中状态

        【讨论】:

        • 不.. 我不认为这就是他想要的。分段控件是完全不同的 UI 元素。他想要一个改变状态的按钮。
        【解决方案8】:

        你试过CompoundButton吗? CompoundButton 具有完全符合您需要的 checkable 属性。用这些替换 ImageButtons。

          <CompoundButton android:id="@+id/buttonhome"
                          android:layout_width="80dp"
                          android:layout_height="36dp"
                          android:background="@drawable/homeselector"/>
        

        将选择器 xml 更改为以下内容。可能需要一些修改,但请务必使用state_checked 代替state_pressed

        <?xml version="1.0" encoding="UTF-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_enabled="false" android:drawable="@drawable/homehover" />
            <item android:state_checked="true" android:state_enabled="true"
                  android:drawable="@drawable/homehover" />
            <item android:state_focused="true" android:state_enabled="true"
                  android:drawable="@drawable/homehover" />
            <item android:state_enabled="true" android:drawable="@drawable/home" />
        
        </selector> 
        

        CompoundButton.OnCheckedChangeListener中,你需要根据你的情况勾选和取消勾选其他。

         mButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        
           public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // Uncheck others. 
                }
           }
        });
        

        同样为每个按钮设置一个OnCheckedChangeListener,当其他按钮被选中时,它会取消选中其他按钮。希望这会有所帮助。

        【讨论】:

        • 您不能直接在 XML 中使用 CompoundButton,因为它是一个抽象类。
        【解决方案9】:

        你可以在 res/drawable 中创建选择器文件

        示例: res/drawable/selector_button.xml

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

        在布局活动、片段等中

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_bg_rectangle_black"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-18
          • 1970-01-01
          相关资源
          最近更新 更多