android开发分享如何设置图像buttonbackgroundimage为不同的状态?

我想要两个状态的图像button我)正常ii)触摸(或点击)。

我已经在imagebutton背景中设置正常的图像,我试图改变 图像(按)onclick方法,但它不会改变。

我想,如果我按下图像button,然后图像应该从正常改变按下,直到我按其他button,但没有发生

任何人都可以向我build议如何使用select器或在运行时间

这是我的imagebuttonpanel代码。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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> 

和我的select器XML是

 <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="https://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事件上更改图片资源,但这并没有帮助。

    在你的drawable中创build一个xml文件,如下所示:

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://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背景。

    尝试这个

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

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

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

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

    相应的select器文件如下所示:

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://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 

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

    嗨尝试下面的代码它会对你有用,

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

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

    你必须补充

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

    到你的图像button。

    默认情况下, onClick是在listitem级别 (父 )处理的。 和imageButtons不接收onClick。

    当你添加上面的属性时,图像button将会收到事件,select器将被使用。

    检查这个解释相同的POSTcheckbox。

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

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

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

    一旦完成。 刚刚到你的代码,并添加此

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

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

      myOLDImageButton.setSelected ( false ) ; 

    我build议你把所有的button引用在一个数组中。

    你想要做的是比imagebutton列表更多的分段button。

    这里; 基本的想法是自定义RadioButton,而不是ImageButton,因为RadioButton将有你需要的检查状态

    你有没有尝试过CompoundButton ? CompoundButton的checkable属性完全符合您的需求。 用这些replaceImageButtons。

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

    将select器xml更改为以下内容。 可能需要一些修改,但一定要使用state_checked代替state_pressed

     <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="https://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设置为每个button, OnCheckedChangeListener中它时将取消选中其他button。 希望这可以帮助。

    即使您使用onClick,@ ingsaurabh的答案也是如此。 但是,如果您正在使用onTouch事件,则可以使用view.setPressed()select不同的背景(仍然与@ingsaurabh的示例相同view.setPressed()

    有关更多详细信息,请参阅以下内容: Android上的“按住”button需要使用onTouchListener更改状态(自定义XMLselect器)

    你可以在res / drawable中创buildselect器文件

    例如:res / drawable / selector_button.xml

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://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"/> 

      以上就是android开发分享如何设置图像buttonbackgroundimage为不同的状态?相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/520753.html

      (0)
      上一篇 2020年12月7日
      下一篇 2020年12月7日

      精彩推荐