更改ImageButton Src图像OnClick

时间:2015-08-28 08:18:23

标签: java android android-imagebutton

我一直在尝试更改我的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);
            }
        });
}

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

  

错误:(34,28)错误:非静态方法setImageResource(int)不能   从静态上下文引用

     

错误:(40,36)错误:非静态方法setImageResource(int)不能   从静态上下文引用

希望你能帮助我。谢谢!

4 个答案:

答案 0 :(得分:1)

您正在尝试将drawable分配给资源属性。改为使用setBackground。

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

我看到的其他内容是ImageButton是一个类,这就是为什么你得到关于静态上下文的错误消息。 您应该使用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开发,现在专注于学习和实验。

答案 1 :(得分:0)

试试这个

imageView.setImageResource(R.drawable.your_icon);

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

imageView.setBackground(..)

这将改变背景而不是src

答案 2 :(得分:0)

您调用的方法setBackgroundResource()希望您将Integer作为参数。在这种情况下,它引用R.mipmap.imgbtn值。而不是写getResources().getDrawable(R.mipmap.imgbtn),只需传递ImageButton.setBackgroundResource(R.mipmap.imgbtn)。

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

答案 3 :(得分:0)

您正在静态访问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);