Android ImageButton | Button | TextView 点击和触摸效果
ImageButton------------------------------------------------>
一、Java 代码:
在drawable目录下添加新的xml文件
button_add_x.xmlimageButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//更改为按下时的背景图片
v.setBackgroundResource(R.drawable.pressed);
}else if(event.getAction() == MotionEvent.ACTION_UP){
//改为抬起时的图片
v.setBackgroundResource(R.drawable.released);
}
return false;
} });
二、1)在drawable目录下添加新的xml文件 button_add_x.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>
2)在布局文件中写
<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>
Button------------------------------------------------>
Java代码:
mBtn = (Button) findViewById(R.id.btn); //btn为layout中的Button ID
mBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0,MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.pressed); //按下的图片对应pressed
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
arg0.setBackgroundResource(R.drawable.normal); //常态下的图片对应normal
}
else if() //这里还可以继续实现MotionEvent.ACTION_MOVE和MotionEvent.ACTION_CANCEL等实现更多的特效
return false;
}
});
TextView------------------------------------------------>
改变字体颜色:和Button的区别是改变的是textColor属性,而且selector文件定义在color - ->
1.在layout文件中指定TextView的textColor属性,如android:textColor="@color/textview_color";
2.在color目录下添加新的xml文件textview_color.xml并指定TextView在各种状态下的色值
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
Android ImageButton | Button | TextView 点击和触摸效果的更多相关文章
- Android之Button自定义点击效果
我们在界面上经常会用到button按钮,但通常button点击后看不到点击的效果,如果用户连续点击了两次,就会报NAR错误,这样交互性就比较差了.如果我们自定义了button点击效果,比如我们点击了b ...
- Android 7.0 TextView点击事件无效修复方案
public class PostTextView extends TextView { private Handler handler = new Handler(Looper.getMainLoo ...
- Android解决button反复点击问题
public class BaseActivity extends Activity { protected boolean isDestroy; //防止反复点击设置的标志.涉及到点击打开其它Act ...
- Android:通过滤镜实现点击图片变暗效果
实现点击图片(ImageView)变暗效果,有一个较简单的方法,就是讲目标图片设置为背景图片(setBackground),再创建一个selector.xml文件,里面放置一张普通状态时的透明图片,一 ...
- [Android]使用button切换TextView可见和不可见
<Button android:id="@+id/btnTest" android:layout_width="match_parent" android ...
- android selector(如对TextView点击样式改变)
selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of Selectabl ...
- Android Listview中Button按钮点击事件冲突解决办法
今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
- android中给TextView或者Button的文字添加阴影效果
1在代码中添加文字阴影 TextView 有一个方法 /** * Gives the text a shadow of the specified radius and color, the ...
- Android textView点击滚动(跑马灯)效果
布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
随机推荐
- .net 邮件批量发送功能源码
#define debug using System; using System.Text; using System.Linq; using System.IO; using System.Ne ...
- PHP实现http与https转化
http://zyan.cc/post/142/ 最近在写PHP程序时,需要使浏览器在https和http之间转化,上网搜索相关信息,无奈只有最近在写PHP程序时,需要使浏览器在https和http之 ...
- 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)
1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...
- java取随机数
一, 指定的特定几个数据集合里按“随机顺序”全部取出 一碰到随机, 可能第一个想到的是用Math.Random() 来处理, 其实java本身提供了现成的类 通过 “打乱顺序”来处理“随机”问题 方法 ...
- vJine.Core 0.3.0.49 正式发布
nuget: https://www.nuget.org/packages/vJine.Core/ oschina: http://git.oschina.net/vjine/vJine.Core/a ...
- OC5_构造方法与self指针
// // Dog.h // OC5_构造方法与self指针 // // Created by zhangxueming on 15/6/9. // Copyright (c) 2015年 zhang ...
- JavaScript基础-对象<1>
1.JavaScript内部对象属性和方法 (1)内置String对象 String 对象是JavaScript的核心对象之一. 创建一个sting对象: var a="this defin ...
- HDU 2993 MAX Average Problem(斜率优化DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...
- 暑假集训(3)第一弹 -----还是畅通工程(hdu1233)
题意梗概:N(n<100)个村子想要富起来,自然就要先修路,不过到底还是没富起来,所以陷入了一个怪圈 :资金不足->修不起路->资金不足...... 为了实现走向全民小康社会,全面实 ...
- Poj/OpenJudge 1094 Sorting It All Out
1.链接地址: http://poj.org/problem?id=1094 http://bailian.openjudge.cn/practice/1094 2.题目: Sorting It Al ...