【Android每日一讲】2012.11.12 设计具有背景图的按钮 - ImageButton的焦点及事件处理

1. 范例说明

  • ImageButton是有背景图片的按钮,要设置ImageButton背景图有许多方法,此程序使用的方法是ImageButton.setImageResource(),需要传递的参数是res/drawable下面的ResourceID
  • 除了设置背景图片的方法外,程序需要使用onFocusChangeonClick等作为按钮事件单击后的处理,最后通过TextView来显示目前图片按钮的状态。

2. 运行结果


3. 编写代码

		mImageButton1.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View arg0, boolean isFocused) {
				// TODO Auto-generated method stub
				/* 若ImageButton状态为onFocus改变ImageButton的图片 * 并改变textView的文字 */
				if (isFocused == true) {
					mTextView1.setText("图片按钮状态为:Got Focus");
					mImageButton1.setImageResource(R.drawable.iconfull);
				}
				/* 若ImageButton状态为offFocus改变ImageButton的图片 *并改变textView的文字 */
				else {
					mTextView1.setText("图片按钮状态为:Lost Focus");
					mImageButton1.setImageResource(R.drawable.iconempty);
				}
			}
		});
		/* 透过onClickListener来响应ImageButton的onClick事件 */
		mImageButton1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/* 若ImageButton状态为onClick改变ImageButton的图片 * 并改变textView的文字 */
				mTextView1.setText("图片按钮状态为:Got Click");
				mImageButton1.setImageResource(R.drawable.iconfull);
			}
		});
		/* 透过onClickListener来响应Button的onClick事件 */
		mButton1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				/* 若Button状态为onClick改变ImageButton的图片 * 并改变textView的文字 */
				mTextView1.setText("图片按钮状态为:Lost Focus");
				mImageButton1.setImageResource(R.drawable.iconempty);
			}
		});

4. 扩展学习与作业

androidButton ImageButton区别

http://www.eyeandroid.com/thread-7064-1-1.html

AndroidImageButton Selector例子

http://www.eyeandroid.com/thread-6645-1-1.html

作业:使用selector来改变ImageButton的背景图片

5.视频讲解与高清下载地址

http://www.eyeandroid.com/thread-10259-1-1.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现具有背景图片的按钮并根据按钮状态显示不同的背景图片,可以使用以下步骤: 1. 在XML布局文件中添加ImageButton控件,并设置背景图片和初始状态的背景图片。 ```xml <ImageButton android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" android:src="@drawable/button_icon" android:state_pressed="false" android:state_selected="false" android:state_enabled="true" /> ``` 2. 在Java代码中获取ImageButton控件,并为其添加状态监听器。 ```java ImageButton myButton = findViewById(R.id.my_button); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 按钮点击事件 } }); myButton.setOnStateChangeListener(new StateListDrawable.OnStateChangeListener() { @Override public void onStateChange(StateListDrawable drawable) { if (drawable.getCurrent().getConstantState() == getResources().getDrawable(R.drawable.button_bg_pressed).getConstantState()) { // 按钮被按下时的背景图片 } else if (drawable.getCurrent().getConstantState() == getResources().getDrawable(R.drawable.button_bg_selected).getConstantState()) { // 按钮被选中时的背景图片 } else { // 按钮正常状态的背景图片 } } }); ``` 3. 在res/drawable文件夹下添加三张不同状态下的背景图片,分别命名为button_bg.xml、button_bg_pressed.xml和button_bg_selected.xml。 button_bg.xml: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/button_bg_selected" android:state_selected="true" /> <item android:drawable="@drawable/button_bg_normal" /> </selector> ``` button_bg_pressed.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF0000" /> <corners android:radius="5dp" /> </shape> ``` button_bg_selected.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#00FF00" /> <corners android:radius="5dp" /> </shape> ``` button_bg_normal.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#0000FF" /> <corners android:radius="5dp" /> </shape> ``` 这样,在Android应用程序中设计具有背景图片的按钮,并根据按钮状态显示不同的背景图片就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值