点击按钮后,更换为另一个图像 [英] Replace one image with another after clicking a button

查看:255
本文介绍了点击按钮后,更换为另一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的xml文件(ImageView的)定义的图像。我想点击我的活动按钮后,将其更改为另一个图像。我该怎么做?

I have defined an image in my xml file (imageview). I'd like to change it to another image after clicking a button in my activity. How can I do that?

感谢您帮助我。

推荐答案

首先实现OnClickListener 接口,您的活动类。
的Java code

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文件

   <?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>

你应该在你的文件夹绘制图标调用和2图像丹

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

这篇关于点击按钮后,更换为另一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆