`

ImageButton 点击与小圆点消息数提示(一些对以前的更新)

阅读更多

我们在定义一个drawable的时候可以通过xml定义的drawable对象。它使得一个图片能在不同的状态下显示不同的图案,比如一个Button,它有pressed,focused,或者其它状态,通过使用state list drawable,你就可以为每种状态提供不同的图片。

 

例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/efax_tab_inbox_disabled" android:state_enabled="false"/>
    <item android:drawable="@drawable/efax_tab_inbox_p" android:state_selected="true"/>
<!--这样被按下后就会切换为另一张图片--!>
    <item android:drawable="@drawable/efax_tab_inbox_p" android:state_pressed="true"/>
    <item android:drawable="@drawable/efax_tab_inbox"/>

</selector>

 

各属性解释如下:

android:drawable 放一个drawable资源
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

 

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。

 

在activity里可以设置按钮的状态

mBtn_efax_received.setSelected(true);
mBtn_efax_failed.setSelected(false);
mBtn_efax_sent.setSelected(false);
mBtn_efax_recycle.setSelected(false);

 

 

设置小圆点消息数提示:

 <ImageButton
                android:id="@+id/imgbtn_inbox"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_margin="2dp"
                android:background="@drawable/selector_efax_tab_inbox"
                />

            <TextView
                android:id="@+id/efax_main_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:layout_alignParentTop="true"
                android:layout_marginTop="10dp"
                android:background="@drawable/indicator_xml"
                android:gravity="center_horizontal"
                android:text="88"
                android:textColor="@color/white"
                android:textIsSelectable="false"
                android:textSize="15sp"
                android:visibility="gone" />
        </RelativeLayout>

 

新建一个drable

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android= "http://schemas.android.com/apk/res/android"
    android:shape= "oval" 
    android:useLevel= "false" >
    <solid android:color= "#c6001d" /> 
    <stroke
        android:width= "1dp"
        android:color= "#c6001d" />
    <size android:width= "20dp"
          android:height= "20dp" /> 
</shape>

 参考:

http://www.oschina.net/question/920274_212245

分享到:
评论

相关推荐

    android imagebutton点击变化 源码

    在Android开发中,`ImageButton`是一个非常常用的控件,它结合了`ImageView`的功能和`Button`的交互性,允许用户通过点击来触发相应的事件。本篇将深入探讨`ImageButton`在点击时如何实现图像的变化,并通过源码解析...

    ImageButton来回切换图片

    在Android开发中,`ImageButton`是一个非常常用的控件,它继承自`Button`,但主要用来显示图像,常用于实现点击按钮时改变图像的效果,比如开关按钮、选择按钮等。本教程将深入探讨如何在`ImageButton`中实现图片的...

    Android 单击选中的ImageButton[]图像数组用法示例.rar

    Android 单击选中的ImageButton[]图像数组用法示例,类似于Radio的功能,不过用图片表现,这样可使Android UI更加友好,视觉更漂亮一些,用户轻触图片,即可选中该数据项,比Radio用户体验更好,如截图所示的选中...

    andoid点击按钮(ImageButton)时改变按钮的背景图片 SelectorDemo

    在本教程中,我们将探讨如何在用户点击`ImageButton`时动态改变其背景图片,这个功能可以通过使用`Selector`来实现。`Selector`是Android中的一个强大的工具,它可以根据组件的状态来改变其外观。 首先,我们需要...

    安卓Android源码——ImageButton.rar

    - `TouchDelegate`: 为了处理大图标的点击区域,`ImageButton` 可能会使用 `TouchDelegate` 来扩大可点击区域,确保用户点击图像边缘也能触发点击事件。 5. **性能优化** - 使用 `Drawable` 对象缓存:`...

    android ImageButton的使用

    在Android开发中,`ImageButton`是一个非常常用的组件,它继承自`ImageView`,但与普通的`ImageView`不同,`ImageButton`允许用户点击,因此常用于实现具有交互功能的按钮。本篇文章将深入探讨`ImageButton`的使用,...

    Android中ImageButton的用法

    与`Button`类似,`ImageButton`也可以添加点击事件监听器,以便在用户点击时执行相应操作: ```java imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //...

    Imagebutton

    【Imagebutton】是一种在Android开发中常用的UI组件,它允许用户通过点击图像来触发相应的操作。在Android的布局文件中,我们通常使用`&lt;ImageButton&gt;`标签来定义它,它是`Button`类的一个子类,继承了`View`和`...

    ImageButton

    ImageButton.msi

    Android ImageButton图片按钮Demo源码.rar

    在上述代码中,我们为`ImageButton`添加了一个点击事件监听器,当用户点击按钮时,会弹出一个短暂的提示消息。 此外,`ImageButton`还可以通过设置`android:tint`属性改变图片的色彩,实现主题色的统一。如果使用...

    Android4.0开发学习之ImageButton

    当用户点击`ImageButton`时,系统会显示一个短暂的提示消息:“ImageButton 被点击了!” 在Android 4.0中,`ImageButton`还支持触摸反馈效果,可以通过设置`android:tint`属性或使用`android:foreground`属性来...

    Flex4 自定义通用ImageButton

    4. **事件处理**:可能包含对鼠标点击和其他交互事件的响应,以实现按钮功能。 接下来,我们看`ImageButtonSkin.mxml`,这是组件的皮肤文件,负责组件的视觉呈现。在Flex4中,皮肤是一种可替换的外观,可以让你在不...

    wpf ImageButton

    在Windows Presentation Foundation (WPF) 中,`ImageButton`是一个自定义控件,它结合了按钮的功能与图像元素,提供了一种以图形化方式触发操作的方式。`ImageButton`控件允许开发人员将图像与按钮功能结合,使用户...

    android ImageButton背景图片的切换

    Android ImageButton 背景图片的切换 Android ImageButton 背景图片的切换是一个常见的交互设计,旨在实现按钮的状态变化。下面我们将详细介绍如何实现 ImageButton 背景图片的切换,并在不同状态实现不同的功能。 ...

    ImageButton的焦点及事件处理

    在Android开发中,`ImageButton`是一个非常常用的组件,它继承自`ImageView`,并增加了对触摸事件的支持。这篇内容将详细解析如何处理`ImageButton`的焦点变化以及响应相关的用户事件。 `ImageButton`与普通的`...

    ImageButton图片按钮

    在Android开发中,`ImageButton`是一个非常常用的组件,它继承自`ImageView`,并增加了点击事件处理的功能。本文将深入探讨`ImageButton`的基本概念、使用方法、属性设置以及实际应用场景,帮助开发者更好地理解和...

    Android中ImageButton的常用方法一览.pdf

    下面将详细讲解`ImageButton`的一些核心概念、属性以及常用方法。 1. `ImageButton`类的定义: `ImageButton`是`ImageView`的子类,因此它具有显示图像的基本功能,同时又具备`Button`的交互特性。在Android框架中...

    Android开发教你使用ImageButton系统图标

    现在,当用户点击`ImageButton`时,会显示一个短暂的提示信息。 在实际应用中,我们可能需要根据不同的状态改变`ImageButton`的外观,例如禁用状态或选中状态。这可以通过修改`android:state_`属性来实现。比如,...

    Android UI编程自定义控件ImageButton

    然而,有时我们可能需要对`ImageButton`进行定制,以满足特定的设计需求或功能扩展。下面我们将详细介绍自定义`ImageButton`的步骤和关键知识点。 1. **自定义控件基础** - 创建新的Java类,继承自`ImageButton`或...

    editText控件和ImageButton控件的配合

    3. **实现交互反馈**:当用户点击`ImageButton`进行搜索或执行其他操作时,可以显示进度条或给出提示信息,增强用户体验。例如,显示一个Toast消息: ```java searchButton.setOnClickListener(new View....

Global site tag (gtag.js) - Google Analytics