开发者

Replace one image with another after clicking a button

I have defined an image in my xml file (imageview). I'd like to change it to another image after clicking a butt开发者_如何学Con in my activity. How can I do that?


First you implements OnClickListener interface to your activity class. Java Code

    package it.codegen.tbx.my;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MytestdroidActivity extends Activity implements OnClickListener {


    Button b1;
    ImageView iw;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       b1 = (Button) findViewById(R.id.button1);
       b1.setOnClickListener(this); 

       iw = (ImageView) findViewById(R.id.icon);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == b1)
        {
            iw.setImageResource(R.drawable.camara);
        }

    }
}

XML file

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

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<ImageView android:id="@+id/icon" android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:src="@drawable/icon"
        />
<Button android:text="Button" android:id="@+id/button1"
 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


</LinearLayout>

you should have 2 images in your drawable folder call icon and camara


public void onClick(View v) {
image1.setImageResource ( R.drawable.another);
}


This is a very basic thing that you are supposed to learn by doing tutorials and reading the getting started docs. Here's a sample:

findViewById(R.id.button).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final ImageView imageView = (ImageView) findViewById(R.id.image_view);
        imageView.setImageResource(R.drawable.some_drawable);
    }
}


Try something like this:

final Button switchButoon = new Button(mContext);
final ImageView imageView = new ImageView(mContext);

switchButoon.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        imageView.setImageResource(R.drawable.icon);
    }
});


For this purpose I'd used ToggleButton and assign custom selector as a background:

<ToggleButton android:background="@drawable/custom_selector>

To avoid modification of the source use a selector, that you can place inside drawable\custom_selector.xml directory i.e.:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/image_pressed" android:state_checked="true" />
    <item android:drawable="@drawable/image" />
</selector>


Its pretty Simple to do,

// First I create a variable named OldImage of type ImageView, and link it to the Layout File's ImageView

 ImageView oldImage = (ImageView) findViewById(R.id.oldImageFileName);

// Create An OnClickListener Method To Update The File Resouce On Click to NewFileName,

 oldImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                oldImage.setImageResource(R.drawable.newFileName);
            }
        });


In Button click listener just set your image like

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
    imageView.setImageResource(R.drawable.your_image);
}});


I hope I can solve it

ImageView imagen = (ImageView)findViewById(R.id.imageViewX);
imagen.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
        ImageView perse = (ImageView) findViewById(R.id.imageViewX);
        perse.setImageResource(R.drawable.fondochange);
     }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜