Android编程动态按钮实现方法
作者:Wallace 时间:2021-12-31 18:05:17
本文实例讲述了Android编程动态按钮实现方法。分享给大家供大家参考,具体如下:
第一种: 该方法通过onTouch来实现,
btn3 = (ImageButton) findViewById(R.id.ImageButton03);
btn3.setOnTouchListener(touchListener3);
View.OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageButton imageBtn = (ImageButton) v;
if(event.getAction() == MotionEvent.ACTION_DOWN){
//更改为按下时的背景图片
imageBtn .setImageResource(R.drawable.pressed);
}else if(event.getAction() == MotionEvent.ACTION_UP){
//改为抬起时的图片
imageBtn .setImageResource(R.drawable.released);
}
return false;
}
};
第二种: 通过XML来实现
用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/button_add" />
<item
android:state_pressed="true"
android:drawable="@drawable/button_add_pressed" />
<item
android:state_focused="true"
android:drawable="@drawable/button_add_pressed" />
<item
android:drawable="@drawable/button_add" />
</selector>
这个文件放在drawable目录下面。命名为button_add_x.xml
使用的时候:
<ImageButton
android:id="@+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_add_x"
>
</ImageButton>
希望本文所述对大家Android程序设计有所帮助。
标签:Android,动态按钮
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot带你实现一个点餐小程序
2022-04-01 23:38:08
![](https://img.aspxhome.com/file/2023/4/64914_0s.png)
详解OpenCV For Java环境搭建与功能演示
2023-05-27 09:13:50
![](https://img.aspxhome.com/file/2023/8/81358_0s.jpg)
C#中WPF ListView绑定数据的实例详解
2023-03-11 12:08:41
![](https://img.aspxhome.com/file/2023/8/104278_0s.png)
Mybatis中的延迟加载案例解析
2023-02-27 01:55:37
![](https://img.aspxhome.com/file/2023/1/68221_0s.png)
springboot中@Async默认线程池导致OOM问题
2022-12-19 20:07:01
![](https://img.aspxhome.com/file/2023/9/103809_0s.png)
Mybatis参数传递示例代码
2023-05-20 09:43:08
Java类加载器ClassLoader用法解析
2023-05-05 21:46:38
Java编程计算兔子生兔子的问题
2023-08-01 15:24:37
spring中使用mybatis plus连接sqlserver的方法实现
2022-11-16 12:05:09
java判断字符串是否为数字的方法小结
2023-11-25 05:54:52
Unity学习之FSM有限状态机
2022-09-10 06:18:33
以实例简介Java中线程池的工作特点
2023-08-11 23:04:19
Android开发笔记之:Handler Runnable与Thread的区别详解
2023-11-10 12:29:01
![](https://img.aspxhome.com/file/2023/7/99807_0s.png)
不看后悔!揭秘游戏服务器开发
2023-08-22 16:20:30
![](https://img.aspxhome.com/file/2023/6/58306_0s.png)
java语言基础之标识符和命名规则详解
2023-04-21 16:50:18
![](https://img.aspxhome.com/file/2023/5/71895_0s.png)
深入分析Java内存区域的使用详解
2023-09-25 23:21:29
Java等待唤醒机制线程通信原理解析
2022-03-31 00:37:21
![](https://img.aspxhome.com/file/2023/7/79867_0s.png)
idea推送项目到gitee中的创建方法
2021-08-19 11:27:14
![](https://img.aspxhome.com/file/2023/7/85437_0s.png)
SpringBoot 自定义starter yaml提示失效问题及解决方法
2022-08-03 14:58:42
![](https://img.aspxhome.com/file/2023/5/70945_0s.jpg)
JAVA线程池原理实例详解
2021-10-01 17:59:01
![](https://img.aspxhome.com/file/2023/2/88522_0s.jpg)