2011年2月17日android按钮开发小知识 ------------------------------------------------------------------------------ android 中的ImageButton和普通的Button区别是有点击状态的不同事件可以重写,还不能设置setText(); 声明时用<ImageButton> 用属性android:src="@drawable/myselector(自己的定义图片的xml放在drawable里)" 自己定义的myselector.xml如下 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android ">
<item android:state_pressed="false" android:drawable="@drawable/back"/> <!-- 设置按钮未被按下时的图片,默认back.png图片 --> <item android:state_pressed="true" android:drawable="@drawable/backdown"/> <!-- 设置按钮按下时的图片,点击时backdown.png图片 --> </selector> ------------------------------------------------------------------------------ 给Activity设定背景: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/bj01(这是存储的一张图片)" > ------------------------------------------------------------------------------- ****小知识****android开发时xml布局文件必须是小写 ------------------------------------------------------------------------------- ImageButton bobButton3 = (ImageButton) findViewById(R.id.bobButton1); bobButton3.setOnClickListener(new OnClickListener() { public void onClick(View v){ ImageButton bobButton3 = (ImageButton)findViewById(R.id.bobButton2);//获取Button控件对象 Intent intent = new Intent(); intent.setClass(BobAbout.this, bobComic.class); startActivity(intent); BobAbout.this.finish(); } }); --------------------------------------------------------------------------------- 其实如果不需要不同点击状态的不同时间,普通的Button也可以设定一个xml来显示不同视觉状态。 只是不能重写不同状态时的方法。