猿问

如何设置不同状态的图像按钮backgroundimage?

如何设置不同状态的图像按钮backgroundimage?

我希望图像按钮有两种状态i)正常ii)触摸(或点击)。

我在图像按钮背景中设置了正常图像,我试图从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>

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


MMMHUHU
浏览 666回答 3
3回答

小怪兽爱吃肉

试试这个btn.setOnClickListener(new&nbsp;OnClickListener()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onClick(View&nbsp;v)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btn.setBackgroundResource(R.drawable.icon); &nbsp;&nbsp;&nbsp;&nbsp;}});

精慕HU

您好,尝试以下代码对您有用,((ImageView)findViewById(R.id.ImageViewButton)).setOnTouchListener(new&nbsp;View.OnTouchListener()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;onTouch(View&nbsp;v,&nbsp;MotionEvent&nbsp;event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(event.getAction()&nbsp;==&nbsp;MotionEvent.ACTION_DOWN) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((ImageView)&nbsp;v.findViewById(R.id.ImageViewButton)).setImageResource(R.drawable.image_over); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(event.getAction()&nbsp;==&nbsp;MotionEvent.ACTION_UP) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((ImageView)&nbsp;v.findViewById(R.id.ImageViewButton)).setImageResource(R.drawable.image_normal); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;}});
随时随地看视频慕课网APP

相关分类

Android
我要回答