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 点击和触摸效果的更多相关文章

  1. Android之Button自定义点击效果

    我们在界面上经常会用到button按钮,但通常button点击后看不到点击的效果,如果用户连续点击了两次,就会报NAR错误,这样交互性就比较差了.如果我们自定义了button点击效果,比如我们点击了b ...

  2. Android 7.0 TextView点击事件无效修复方案

    public class PostTextView extends TextView { private Handler handler = new Handler(Looper.getMainLoo ...

  3. Android解决button反复点击问题

    public class BaseActivity extends Activity { protected boolean isDestroy; //防止反复点击设置的标志.涉及到点击打开其它Act ...

  4. Android:通过滤镜实现点击图片变暗效果

    实现点击图片(ImageView)变暗效果,有一个较简单的方法,就是讲目标图片设置为背景图片(setBackground),再创建一个selector.xml文件,里面放置一张普通状态时的透明图片,一 ...

  5. [Android]使用button切换TextView可见和不可见

    <Button android:id="@+id/btnTest" android:layout_width="match_parent" android ...

  6. android selector(如对TextView点击样式改变)

    selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of Selectabl ...

  7. Android Listview中Button按钮点击事件冲突解决办法

    今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...

  8. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  9. Android textView点击滚动(跑马灯)效果

    布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

随机推荐

  1. SQL server 的约束条件【转】

    SQLServer - 约束 一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是 ...

  2. 将Cent0S 7的网卡名称eno16777736改为eth0

    新建的虚拟机redhat linux7默认的网卡名称是eno16777736,找不到eth0如图所示: 修改网卡名称: 输入如下命令,进入对应目录,编辑文件: vim /etc/sysconfig/g ...

  3. 【HTTPS】Https和SSL学习笔记(一)

    1. 什么是HTTPS 在说HTTPS之前必须要先说一下HTTP.我们平常浏览网页用的就是HTTP协议,HTTP协议之间传输的数据都是明文,这样对于一些敏感信息传输其实是不安全的,很容易被恶意窃取.应 ...

  4. Oracle归档已满的处理办法

    SqlPlus: / as sysdba select * from V$FLASH_RECOVERY_AREA_USAGE; show parameter log_archive_dest; sho ...

  5. Android系统简介(中):系统架构

    Android的系统架构栈分为4层,从上往下分别是Applications.Application framework.Libraries  & Android Runtime.Linux  ...

  6. 用PL0语言求Fibonacci数列前m个中偶数位的数

    程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...

  7. android中的4种点击事件

    四种点击事件     (1)采用内部类的方式去实现OnClickListener      (2)匿名内部类     (3)当前类imp OnClickListener      (4)onclick ...

  8. 恶心的学校机房SQL安装

    学校机房每台PC(DELL OPTIPLEX 380)上有两个系统,分别对应XP中英文版.管理员将500G硬盘分为两部分(两个主分区,两个逻辑分区),每个系统占用一个主分区和一个逻辑分区,主分区都有冰 ...

  9. 【转】C#中没有id 没有name C#怎么点击按钮

    HTML按钮元素 <input type="submit" value="确定" class="dialogbtn" C# 执行代码 ...

  10. VC++2010添加菜单

    1  资源视图下面右键添加资源 选择menu 2  创建你想要的menu 3  找到CDialog::OnInitDialog();在后面添加代码. CMenu menu; menu.LoadMenu ...