`
poyunri
  • 浏览: 29423 次
文章分类
社区版块
存档分类
最新评论

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来实现:

将下面的文件放在drawable目录下面。命名为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>
使用的时候:

<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>

下面详细介绍下几种触发属性:

1)android:state_pressed

Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.

如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

2)android:state_focused

Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.

true,获得焦点时显示;false,没获得焦点显示默认。

3)android:state_selected

Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.

true,当被选择时显示该图片;false,当未被选择时显示该图片。

4)android:state_checkable

Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

5)android:state_checked

Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.

true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

6)android:state_enabled

Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.

true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。

7)android:state_window_focused

Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。

分享到:
评论

相关推荐

    Android利用悬浮按钮实现翻页效果

    今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子。 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.LayoutParams。那么在AndroidManifest.xml中添加权限: &lt;uses ...

    Android TV开发:实现3D仿Gallery效果的实例代码

    Android高级图片滚动控件实现3D版图片轮播器这篇文章,我是基于他的代码进行修改的,主要为了移植到电视上做了按键事件和焦点控制。 3.具体代码: public class Image3DSwitchView extends LinearLayout { ...

    Android代码-利用Android AccessibilityService 实现自动加好友,拉人进群聊

    版本的按键精灵,其实就是点点点,本项目仅用于技术研究学习之用,请勿用于商业用途,所以项目不会做任何适配! 只保证在笔者手机上是可以正常运行的,代码开源,有问题或者建议欢迎提issues。运行项目后,需要启用...

    Android 实现机顶盒手势、数据分页功能.rar

    Android 实现机顶盒手势、数据分页功能,本源码需要800*480分辨率的模拟器才可运行。DEMO源码内使用到的封装类,包括一个分页控件、一个列表控件GridView 、一个支持动画效果的ViewFlipper。下载此源码的朋友可以从...

    Android ListView左右滑动删除Item效果

    Android 使用NineOldAndroids开源UI框架实现绚丽的ListView左右滑动删除Item效果。欢迎下载。

    Android App实现监听软键盘按键的三种方式

    我们在Android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“Go”按键加载url页面;在点击搜索框的时候,点击右下角的search符号键可以进行搜索;或者在...

    Android实现模仿UCweb菜单效果的方法

    本文实例讲述了Android实现模仿UCweb菜单效果的方法。分享给大家供大家参考。具体如下: UCWeb的菜单看起来不错,自己模仿做一个,思路实现如下: 1、保留menu按键作用 2、用popupwindow作为菜单显示容器 3、用...

    Android 状态栏虚拟导航键透明效果的实现方法

    主要介绍了Android 状态栏虚拟导航键透明效果的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

    android实现RecyclerView列表单选功能

    本文实例为大家分享了android实现RecyclerView列表单选功能的具体代码,供大家参考,具体内容如下 实现思维 1.首先在一行的xml布局中添加一个选中效果的icon图片,未选中的情况下INVISIBLE或者GONE 都可以,推荐使用...

    Android隐藏和沉浸式虚拟按键NavigationBar的实现方法

    上图为沉浸式虚拟按键效果。 上图为隐藏虚拟按键效果。 那我们先说如何隐藏虚拟按键: public static void hideNavKey(Context context) { if (Build.VERSION.SDK_INT &gt; 11 && Build.VERSION.SDK_INT &lt; 19) { ...

    Android实现双击返回键退出应用实现方法详解

    效果图 实现 第一种办法 响应Activity的 onKeyUp事件,两次点击时间大于2秒钟就不响应,小于2秒钟就退出程序 代码: //记录用户首次点击返回键的时间 private long firstTime = 0; /** * 第一种解决办法 通过...

    Android实现九宫格解锁的方法

    相信大家都有使用九宫格解锁,比如在设置手机安全项目中,可以使用九宫格解锁,提高安全性,以及在使用支付功能的时候,为了提高安全使用九宫锁,今天就为大家介绍Android实现九宫格的方法,分享给大家供大家参考。...

    Android应用开发中模拟按下HOME键的效果(实现代码)

    下面提供代码示例,请参考。 代码如下:public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(Intent.ACTION_MAIN);...

    Android仿考拉全局滑动返回及联动效果的实现方法

    分析其主要原因应该是android已有实体的返回按键,这样的功能变得不重要,但我觉得有这样的功能便于单手操作,能提升app的用户体验,特别是从ios转到android的用户。写这篇博文希望可以对大家有所帮助,希望自己的...

    Android 仿微信自定义数字键盘的实现代码

    本文介绍了Android 仿微信自定义数字键盘的实现代码,分享给大家,希望对大家有帮助 最终效果: 实现这个自定义键盘的思路很简单: 要写出一个数字键盘的布局; 与 Edittext 结合使用,对每个按键的点击事件进行...

    解决Android 沉浸式状态栏和华为虚拟按键冲突问题

    这是我自己的手机,OnePlus 3T 7.1.1版本(免费广告,没给我钱的啊),不是华为的手机,但是有个虚拟按键可以设置,可以看到底部导航栏没有问题,顶部状态栏也成功实现,效果图看完,下面直接飙车了: 主页面布局: ...

    Android代码-与电子琴相像的自由演奏软件

    使用android studio开发的一个简单的安卓项目,与电子琴相像的自由演奏软件。 (自由演奏模式)第一个界面:通过点击 7 个不同的数字按钮,实现哆啦咪发唆等 7 个音阶 的播放。通过这个简易功能,用户可以按照自己...

    Android应用开发入门教程

    第1章 Android的系统介绍5 1.1 系统介绍5 1.2 软件结构和使用的工具7 第2章 Android SDK的开发环境10 2.1 Android SDK的结构10 2.2 Android SDK环境安装11 2.2.1. 安装JDK基本Java环境11 ...10.4 3D动画效果的实现129

    利用libmp3lame实现在Android上录音MP3文件示例

    之前项目需要实现MP3的录音,于是使用上了Lame这个库。这次做一个demo,使用AndroidStudio+Cmake+NDK进行开发。利用Android SDK提供的AndroidRecorder进行录音,得到PCM数据,并使用jni调用Lame这个C库将PCM数据转换...

Global site tag (gtag.js) - Google Analytics