Android 单击按钮后更改ImageView源一段时间
我有两个按钮和一个图像视图:Android 单击按钮后更改ImageView源一段时间,android,button,imageview,Android,Button,Imageview,我有两个按钮和一个图像视图: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" xmln
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >
<ImageView
android:id="@+id/dog"
android:contentDescription="@string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/dogsec" />
<ImageButton
android:id="@+id/barkk"
android:contentDescription="@string/desc"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/dog"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:src="@drawable/bark" />
<ImageButton
android:id="@+id/rrrr"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/barkk"
android:layout_below="@+id/barkk"
android:layout_marginRight="41dp"
android:layout_marginTop="15dp"
android:contentDescription="@string/desc"
android:src="@drawable/rrr" />
</RelativeLayout>
a) 我需要让Imageview将其源更改为另一个源(我的意思是图像的默认状态是picture1,它应该显示picture2并返回picture1)点击按钮“barkk”。只要按下巴克键,它就不应显示图2
b) 只要按下按钮rrrr,我就需要显示图片3
我使用的正是ImageView,它应该根据上述情况a)或b)更改其源
请帮帮我。提前感谢使用此代码
myButton.setOnClickListener(new OnClickListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN) {
myImageView.setImageResource(R.drawable.myNewImage);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
myImageView.setImageResource(R.drawable.myNewImage);
}
}
};
您需要选择器xml文件,请访问@Shayanpourvatan“我使用的正是ImageView,它应该根据上述情况a)或b)更改其来源。”这就是选择器不适用的原因,没有按钮,但ImageView Only为什么不适用?在选择器文件中更改
drawable
,并将其设置为图像的背景,如果您不想这样做,您需要使用onTouchListener
@Shayanpourvatan在代码中处理它,您想说可以通过选择器将单独的图像视图与按钮连接吗?您想通过单击按钮更改另一个图像吗?所以不,这是不可能的,你需要在代码上这样做