为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

MMMHUHU 2019-08-27 11:14:12
如何设置不同状态的图像按钮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事件上更改图像资源,但它没有帮助。
查看完整描述

3 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

试试这个

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


查看完整回答
反对 回复 2019-08-27
?
精慕HU

TA贡献1845条经验 获得超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;
    }});


查看完整回答
反对 回复 2019-08-27
  • 3 回答
  • 0 关注
  • 665 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信