1.ImageButton和普通的Button区别就在于外观更好看,可以用一个图片当做一个Button,当然也会有配套的UI设计方案。
声明方法和普通的Button差不多,只不过这里引用了另一个xml文件作为这个Button切换的效果布局
<ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/imagebutton01" />
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/up"/> <item android:state_focused="true" android:drawable="@drawable/down"/> <item android:drawable="@drawable/normal"/> </selector>
表示当按钮压下去时,这个ImageButton应该是什么样的。
android:state_focused="true"表示当按钮获取焦点的时候,这个ImageButton应该是什么样的。
但是如果只是设置成这样样子,在Activity中使用setOnFocusChangeListener是没有效果的,需要再加上点标签才可以:
android:focusable="true" android:focusableInTouchMode="true"监听事件为:
ib1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "ImagButton被点击了", 3333).show(); } }); ib1.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "ImagButton获取了焦点", 3333).show(); } });
引用这个xml的标签两者也有不同:
android:listSelector="@drawable/mylist_view"
android:background="@drawable/mylist_view"
listView以上两种方法都可以使用只不过第一个是在listView中,第二个是在Item中。但是Button只能使用第二种。