更改 ImageButton Src 图像 OnClick

Changing ImageButton Src Image OnClick

我一直在尝试更改我的 ImageButton 源 OnClick 并在 500 毫秒后将其更改回来。这是代码(还有更多 ImageButtons,这只是其中一个 ImageButtons 的完整示例):[更新]

package com.example.apptest;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.mainactivitylayout);

        ImageButton Next=(ImageButton)findViewById(R.id.nexticon);
        // Here there are more buttons //
        
        Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            Intent splash = new Intent(MainActivity.this, NextActivity.class);
                startActivity(splash);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                ImageButton.setImageResource(R.mipmap.imgbtnonclick);
                Handler h =new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.nexticon);
                        ImageButton.setImageResource(R.mipmap.imgbtn);
                    }
                }, 500);
            }
        });
}
 

它不起作用,消息等级生成说:[更新]

Error:(34, 28) error: non-static method setImageResource(int) cannot be referenced from a static context

Error:(40, 36) error: non-static method setImageResource(int) cannot be referenced from a static context

希望你能帮助我。谢谢!

您正在尝试将可绘制对象分配给资源 属性。请改用 setBackground。

ImageButton.setBackground(getResources().getDrawable(R.mipmap.imgbtn));

我还看到 ImageButton 是一个 class,这就是为什么您会收到有关静态上下文的错误消息。 您应该使用 view.findViewById()

参考要更改图像的按钮

您可以使用 ButterKnife 轻松绑定按钮并将方法应用于按钮组。

绑定按钮:

@Bind({R.id.nexticon, R.id.nexticon2, R.id.nexticon3, R.id.nexticon4}) List<View> allButtons;

定义动作:

static final ButterKnife.Action<View> changeImageSource = new ButterKnife.Action<View>() {
        @Override public void apply(View view, int index) {
             ImageButton.setImageResource(R.mipmap.imgbtnonclick);
                Handler h =new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.nexticon);
                        ImageButton.setImageResource(R.mipmap.imgbtn);
                    }
                }, 500);
        }
    };

执行动作:

Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            Intent splash = new Intent(MainActivity.this, NextActivity.class);
                startActivity(splash);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
              ButterKnife.apply(allButtons, ADDLISTENER);
            }
        });

我认为还是使用 xml 中定义的背景选择器,然后临时更改按钮的状态更好。这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/buttonok_pressed"
          android:state_enabled="false" />

    <item android:drawable="@drawable/buttonok" />

</selector>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
       android:minWidth="@dimen/button_minwidth"
    >

    <corners android:radius="@dimen/button_radius"/>

    <gradient
    android:angle="45"
        android:centerX="35%"
        android:centerColor="#0070DC"
        android:startColor="#00A0E2"
        android:endColor="#0001CF"
        android:type="linear"
    />

    <padding
        android:left="@dimen/button_paddingH"
        android:top="@dimen/button_paddingV"
        android:right="@dimen/button_paddingH"
        android:bottom="@dimen/button_paddingV"
        />


    <stroke
        android:width="2dp"
        android:color="#0000FF"
    />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
       android:minWidth="@dimen/button_minwidth">

    <corners android:radius="@dimen/button_radius"/>

    <gradient
        android:angle="45"
        android:centerX="35%"
        android:centerColor="#0097FC"
        android:startColor="#00D5FF"
        android:endColor="#0011FF"
        android:type="linear"
    />

    <padding
        android:left="@dimen/button_paddingH"
        android:top="@dimen/button_paddingV"
        android:right="@dimen/button_paddingH"
        android:bottom="@dimen/button_paddingV"
    />

    <stroke
        android:width="2dp"
        android:color="#0000FF"
    />

</shape>

另外,我建议您不要追求快速的结果,或者只是让它起作用并尝试不同的东西。我猜您正在使用 Android 开始开发,现在专注于学习和试验。

像这样尝试

imageView.setImageResource(R.drawable.your_icon);

imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_icon));

imageView.setBackground(..)

这将更改背景而不是 src

您调用的方法 setBackgroundResource() 要求您提供一个 Integer 作为参数。在本例中,它指的是 R.mipmap.imgbtn 值。不要写 getResources().getDrawable(R.mipmap.imgbtn),只需传递 ImageButton.setBackgroundResource(R.mipmap.imgbtn).

此外,您应该将 imgbtn 移动到可绘制文件夹而不是 mipmap(用于启动器图标(Then the code would begetResources().getDrawable(R.drawable.imgbtn)`

您正在静态访问 HandlerImageButton

而不是:

 Handler=new Handler();
            Handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ImageButton.setBackgroundResource(getResources().getDrawable(R.mipmap.imgbtn));
                }
            }, 500);

使用:

Handler h =new Handler();
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
          //Here ImageButton also should be initialized (findViewByd(R.id.yourImageButton)) 
          ((ImageButton) view.findViewById(R.id.yourImageView)).setBackgroundResource(getResources().getDrawable(R.mipmap.imgbtn));
                }
            }, 500);