设计具有背景图的按钮(ImageButton的焦点及事件处理)

来源:互联网 发布:淘宝模特招聘要求 编辑:程序博客网 时间:2024/06/09 15:46

设计具有背景图的按钮(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);

              }

           }

       });

 

0 0
原创粉丝点击