在java android中快速更改图像

时间:2014-09-18 11:40:58

标签: java android image onclicklistener

我在点击时快速更改图像时遇到问题。我已经制作了一个图像按钮,我想在单击按钮并返回上一个图像时更改图像。我搜索了很多,但无法找到解决方案。

public class Buttonone extends Activity {
    ImageButton btonImage;
    boolean isPressed = true;
    MediaPlayer mp;
    boolean isPlay = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_one);

        btonImage = (ImageButton) findViewById(R.id.btonImage);
        btonImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                playSound();toggleImage();
                }

        });
    }

    private void toggleImage() {
        // TODO Auto-generated method stub
        btonImage.setImageResource(R.drawable.image2);
    }
}

这是图像按钮的布局xml

    <ImageButton
android:id="@+id/btonImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:background="#080808"   
android:src="@drawable/image1" />

2 个答案:

答案 0 :(得分:2)

将src设置为可绘制的

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/imgButtonBack"/>

在drawable文件夹中创建一个名为imgButtonBack.xml的文件,并设置以下代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@android:drawable/presence_video_away"
       android:state_pressed="true" />
   <item android:drawable="@android:drawable/presence_audio_busy"
      android:state_focused="true" />
   <item android:drawable="@android:drawable/presence_audio_busy" />
</selector>

根据需要更改图像

答案 1 :(得分:0)

的setContentView(R.layout.button_one);

这是你的问题。您应该设置整个布局并处理点击事件!

相关问题