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

自定义点击效果有两种方式,一种是在xml中定义,另一种是在代码中定义。

首先看一下如何在xml中定义:

在drawable下新建selector.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/button_press" android:state_pressed="true"/>
  4. <item android:drawable="@drawable/button_nomal" android:state_focused="false" android:state_pressed="false"/>
  5. <item android:drawable="@drawable/button_focus"  android:state_focused="true"/>
  6. <item android:drawable="@drawable/button_nomal" android:state_focused="false"/>
  7. </selector>

定义了两种状态 一种是按下  一种是获得焦点。  drawable分别引用了这三张图片

然后在main.xml下添加button按钮

  1. <Button
  2. android:id="@+id/button1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="button效果演示"
  6. android:background="@drawable/selector" />

在MainActivtiy中得到button

  1. Button button1=(Button) this.findViewById(R.id.button1);
  2. button1.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. // TODO Auto-generated method stub
  6. Toast.makeText(getApplicationContext(), "你点击了button按钮", Toast.LENGTH_SHORT).show();
  7. }
  8. });

下面看下点击效果:

点击button前:

当按下button按钮时:

接下来 看下第二种实现方式,在代码中实现:

首先在main.xml中添加:

  1. <Button
  2. android:id="@+id/button2"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="button效果演示"
  6. android:background="@drawable/button_nomal"/>

接下面在MainActivity中实现:

  1. Button button2=(Button) this.findViewById(R.id.button2);
  2. button2.setOnTouchListener(new OnTouchListener() {
  3. @Override
  4. public boolean onTouch(View v, MotionEvent event) {
  5. // TODO Auto-generated method stub
  6. if(event.getAction()==MotionEvent.ACTION_DOWN){
  7. v.setBackgroundResource(R.drawable.button_press);
  8. }else if(event.getAction()==MotionEvent.ACTION_UP){
  9. v.setBackgroundResource(R.drawable.button_nomal);
  10. }
  11. return false;
  12. }
  13. });

在这类绑定了button的OnTouchListener监听,因为OnClickListener继承了OnTouchListener。运行效果和上面一样,这里不做过多解释。

Android之Button自定义点击效果的更多相关文章

  1. Android 纯代码加入点击效果

    项目中非常多的Button, 同一时候配置非常多button切图,Selector是不是非常烦, 使用以下这个类,就能够直接为Button添加点击效果. 不用多个图片,不用Selector. 使用方法 ...

  2. Android checkbox 自定义点击效果

    安卓默认的效果                         自定义后的效果 前面的图片当然可以自己修改. 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml ver ...

  3. Android MaterialDesign之水波点击效果的几种实现方法

    什么是水波点击的效果? 下面是几种不同的实现方法的效果图以及实现方法   Video_2016-08-31_003846 如何实现? 方法一 使用官方提供的RippleDrawable类 优点:使用方 ...

  4. Android之水波纹点击效果(RippleView)

    Android5.0后各种炫的效果纷纷出来,写这篇博客主要是讲的是按钮点击效果带有的水波纹(波浪式). 当然我写的这个是自定义来实现的,在低版本(5.0一下)也可以实现点击效果.看看效果图: 上图可看 ...

  5. 如何让LinearLayout也有类似Button的点击效果?

    有的时候,我们希望LinearLayout布局也有点击的效果,这时候我们不仅需要一个作为背景的selector,还要设置一些其它属性才行: android:clickable="true&q ...

  6. android 总结(样式)—跑马灯 button的点击效果 RadioGroup 实现滑动的效果 button 下面有阴影 卡片样式

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...

  7. Android ImageButton | Button | TextView 点击和触摸效果

    ImageButton------------------------------------------------> 一.Java  代码: 在drawable目录下添加新的xml文件 bu ...

  8. android自定义控件(4)-自定义水波纹效果

    一.实现单击出现水波纹单圈效果: 照例来说,还是一个自定义控件,观察这个效果,发现应该需要重写onTouchEvent和onDraw方法,通过在onTouchEvent中获取触摸的坐标,然后以这个坐标 ...

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

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

随机推荐

  1. eclipse关联源码 Failed to create the part's controls

    在eclipse中关联源码时: 1.出现Failed to create the part's controls错误, 2.而attach source 这个按钮却没有出现. 问题原由:eclipse ...

  2. Spring mvc源码url路由-我们到底能走多远系列(38)

    我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...

  3. metronic后台模板学习 -- 所用外部插件列表

    插件名称 描述 URL jQuery 1.11.0 js库,不用介绍了 http://www.jquery.com jQuery Migrate plugin 1.2.1 jQuery 老版本过渡迁移 ...

  4. archlinux安装图形界面

    安装xorg 检查显卡驱动 $ lspci | grep VGA install X server wiki Then use the $ startx to start the X server, ...

  5. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  6. map的相关

    private static final Map<String, String> flagMap = new HashMap<String, String>(); static ...

  7. Matlab位运算操作

    本文为转载他人文章: bitand 按位与操作 a = 7; b = bitand(10,a); disp(dec2bin(a,8)); %ans = 00000111 disp(dec2bin(b, ...

  8. 111. Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. ehcache 缓存

    一:详细配置步骤 1,添加ehcache.xml文件 将ehcache.xml文件添加到src路径下面.ehcache.xml文件内容如下 <ehcache> <diskStore  ...

  10. ListView中convertView和ViewHolder的工作原理

    http://blog.csdn.net/bill_ming/article/details/8817172