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 等待类型

    下表列出各任务所遇到的等待类型. 等待类型 说明 ASYNC_DISKPOOL_LOCK 当尝试同步并行的线程(执行创建或初始化文件等任务)时出现. ASYNC_IO_COMPLETION 当某任务正 ...

  2. Git的安装以及注册账号等操作

    1.安装Git-2.5.1-64-bit.exe  一直下一步直至完成 2.注册github账号 官网地址:https://github.com/github 3.找到一个按钮“New Reposit ...

  3. JAVA笛卡尔曲线

    效果图如下: import java.applet.*; import java.awt.*; public class Descartes extends Applet { int AppletWi ...

  4. 模板方法模式(Template Pattern)

    模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法的结构下,重新定义算法中的某些步骤. 这个模式是用来创建一个算法模板.模板就是一个方法.更具体地 ...

  5. Linux下Qt环境的搭建

    之前一直使用Ubuntu软件中心中的Qt4开发Qt的应用程序,现在转到Linux下来做Qt5开发,但是必须从Qt官网上面下载对应的安装包,配置起来相对麻烦一些,这里介绍整个开发流程. 首先,在官网上面 ...

  6. preg_match_all, preg_match

    int preg_match(string $pattern, string $subject[, $arr][, int $flags]);$pattern 正则表达式$subject: 要搜索的字 ...

  7. [转] Js获取 本周、本月、本季度、本年、上月、上周、上季度、去年时间段

    /** * 针对时间的工具类 */ var DateTimeUtil = function () { /*** * 获得当前时间 */ this.getCurrentDate = function ( ...

  8. php中utf8 与utf-8 与utf8 无BOM

    utf8 与utf-8 相信很多程序员刚开始也会有这样的疑惑,如题,我也是.    其实,他们可以这样来区分.    一.在php和html中设置编码,请尽量统一写成“UTF-8”,这才是标准写法,而 ...

  9. Oracle索引扫描

    Oracle索引扫描:先通过index查找到索引的值,并根据索引的值对应的rowid值(对于非唯一索引可能返回多个rowid值)直接从表中得到具体的数据.一个rowid唯一的表示一行数据,该行对应的数 ...

  10. RAC,客户端连接失败ORA-12514

    今天上午,某项目运维组的同事过来求助:"某系统的应用有问题了,WEB页面打开以后出现ORACLE的ORA-12514错误,貌似监听有问题了!" 该系统的数据是采用RAC部署的模式, ...