android中的ImageButton

2024-05-07 03:18
文章标签 android imagebutton

本文主要是介绍android中的ImageButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

/** Copyright (C) 2007 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.widget;import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.RemoteViews.RemoteView;import java.util.Map;/*** <p>* Displays a button with an image (instead of text) that can be pressed * or clicked by the user. By default, an ImageButton looks like a regular * {@link android.widget.Button}, with the standard button background* that changes color during different button states. The image on the surface* of the button is defined either by the {@code android:src} attribute in the* {@code <ImageButton>} XML element or by the* {@link #setImageResource(int)} method.</p>* * <p>To remove the standard button background image, define your own * background image or set the background color to be transparent.</p>* <p>To indicate the different button states (focused, selected, etc.), you can* define a different image for each state. E.g., a blue image by default, an* orange one for when focused, and a yellow one for when pressed. An easy way to* do this is with an XML drawable "selector." For example:</p>* <pre>* <?xml version="1.0" encoding="utf-8"?>* <selector xmlns:android="http://schemas.android.com/apk/res/android">*     <item android:state_pressed="true"*           android:drawable="@drawable/button_pressed" /> <!-- pressed -->*     <item android:state_focused="true"*           android:drawable="@drawable/button_focused" /> <!-- focused -->*     <item android:drawable="@drawable/button_normal" /> <!-- default -->* </selector></pre>** <p>Save the XML file in your project {@code res/drawable/} folder and then * reference it as a drawable for the source of your ImageButton (in the * {@code android:src} attribute). Android will automatically change the image * based on the state of the button and the corresponding images* defined in the XML.</p>** <p>The order of the {@code <item>} elements is important because they are* evaluated in order. This is why the "normal" button image comes last, because* it will only be applied after {@code android:state_pressed} and {@code* android:state_focused} have both evaluated false.</p>** <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a>* guide.</p>** <p><strong>XML attributes</strong></p>* <p>* See {@link android.R.styleable#ImageView Button Attributes},* {@link android.R.styleable#View View Attributes}* </p>*/
@RemoteView
public class ImageButton extends ImageView {public ImageButton(Context context) {this(context, null);}public ImageButton(Context context, AttributeSet attrs) {this(context, attrs, com.android.internal.R.attr.imageButtonStyle);}public ImageButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setFocusable(true);}@Overrideprotected boolean onSetAlpha(int alpha) {return false;}@Overridepublic void onInitializeAccessibilityEvent(AccessibilityEvent event) {super.onInitializeAccessibilityEvent(event);event.setClassName(ImageButton.class.getName());}@Overridepublic void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {super.onInitializeAccessibilityNodeInfo(info);info.setClassName(ImageButton.class.getName());}
}

这篇关于android中的ImageButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966166

相关文章

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局