2011-01-21 62 views
77

我想要两个状态的图像按钮i)正常ii)触摸(或点击)。如何设置图像按钮backgroundimage为不同的状态?

我已在的ImageButton背景正常形象,我从的onclick方法试图变化图像(按下),但它不会改变。

我想如果我按下图像按钮,然后图像应该从正常变为按下,直到我按下其他按钮,但它不会发生

任何人都可以向我建议我怎样才能做到这一点选择或在运行时

这是我的imagebuttonpanel代码。

<?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事件图像资源,但它并不能帮助。

+0

我认为你看ing [像这样](http://stackoverflow.com/questions/3097710/android-change-button-background-programmatically/3097844#3097844)。我之前曾问过类似的问题。 – 2011-01-21 06:05:15

回答

214

这样创建您绘制的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的背景。

+11

正确。这将被称为选择器XML – 2011-01-21 06:32:50

+4

@Hitendra:我想操作已经知道这一点。该问题可以解释为选择器没有被调用的原因。 – Samuel 2011-08-23 03:10:52

+1

它为我工作超级,谢谢lot – Naruto 2012-07-19 19:20:20

8

喜试试下面的代码将是对你有用,

((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; 
    } 
}); 
14

试试这个

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

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

你必须

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

添加到您的图像按钮。

默认情况下,onClick在列表级别(父级)处理。和imageButtons不会收到onClick。

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

检查此POST这说明复选框相同。

1

如果你想挤压的图像按钮,然后形象应该是从正常到改变按下

但我最好的方法将是自定义单选按钮和一个组中使用它们。我看到了一个例子。 对不起,我不记得那个链接。

但如果你想避免这一点。 您需要将其添加到您的selector.xml中

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

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

你会看到结果。但是你必须管理最近按下按钮的状态。所以你可以设置

myOLDImageButton.setSelected (false) ; 

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

0

你试过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每个按钮,当检查这将取消其它按钮。希望这可以帮助。

8

mhh。我得到了另一个解决方案,它帮助了我,因为我有两个不同的张祖兴:

  • 要么项目不点击
  • 或者被点击,所以它会被高亮

在我.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 

做起来难。希望这可以帮助。花了我一段时间才弄清楚。

0

你可以创建资源选择文件/绘制

例如: RES /绘制/ 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"/> 
相关问题