设计具有背景图的按钮(ImageButton的焦点及事件处理)
新建一个继承Activity类的ImageButtonChangeBackgroundActivity,并设置布局文件为:imagebuttonchangebackground.xml。
首先为布局文件添加一个ImageButton。这里使用2张图片
|
<ImageButton android:id="@+id/imagebuttonchangebackground" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a3" /> |
而后在Activity代码中取得ImageButton,并为ImageButton设置焦点事件。
|
this.imageButton = (ImageButton) super .findViewById(R.id.imagebuttonchangebackground_ibtn); this.imageButton.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { imageButton.setImageResource(R.drawable.a2); } else { imageButton.setImageResource(R.drawable.a3); } } }); |
本文介绍如何通过设置ImageButton的焦点改变事件来实现按钮背景图的切换效果。具体步骤包括创建ImageButton并设置默认图片,然后通过OnFocusChangeListener监听焦点变化,进而更换图片资源。
386

被折叠的 条评论
为什么被折叠?



