①,将2张图片放入 res -> drawable 中,如下所示:
创新互联是一家专业的成都网站建设公司,我们专注网站建设、成都网站建设、网络营销、企业网站建设,外链,1元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。
②, 在UI中加入ImageView组建,方法将ImageView拖入到设计窗,弹出选择图片框(UI)
②-1 , 拖ImageView到UI设计窗
②-2 ,在弹出的“Resources”框中中,选择需要显示的图片,如下
①,这个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 );
}
});
}
}
①,初始状态
②,点击按钮后