一 : 准备图片
①,将2张图片放入 res -> drawable 中,如下所示:
②, 在UI中加入ImageView组建,方法将ImageView拖入到设计窗,弹出选择图片框(UI) ②-1 , 拖ImageView到UI设计窗 ②-2 ,在弹出的“Resources”框中中,选择需要显示的图片,如下
二 :写代码,当点击btn后,换一张图片
①,这个ImageView的Id为 img_tree ②,代码
package com.kayer.myfirstdemo;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txt_t = null;
private Button btn_ok = null;
private ImageView img_tree = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txt_t = (TextView)this.findViewById(R.id.txt_t);
this.btn_ok = (Button)this.findViewById(R.id.btn_ok);
this.img_tree = (ImageView)this.findViewById(R.id.img_tree);
this.btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence a = MainActivity.this.txt_t.getText();
if( a.toString() == "Hello World!" ){
MainActivity.this.txt_t.setText( "Aonaufly" );
}else{
MainActivity.this.txt_t.setText( "Hello World!" );
}
Drawable icon_1 = getDrawable( R.drawable.tree123 );
MainActivity.this.img_tree.setImageDrawable( icon_1 );
}
});
}
}
三:安装到真机中测试
①,初始状态
②,点击按钮后