在Android中,您可以使用“ android.widget.ImageButton ”显示具有自定义背景图片的常规“ Button ”。

在本教程中,我们向您展示如何显示一个带有名为“ android_button.png ”的背景图像的按钮,当用户单击它时,显示一条短消息。 就如此容易。

注意
您可能还喜欢这个Android ImageButton选择器示例 ,该示例允许根据按钮状态更改按钮的图像。

PS此项目在Eclipse 3.7中开发,并通过Android 2.3.3进行了测试。

1.将图像添加到资源

将图像“ android_button.png ”放入“ res / drawable-?dpi ”文件夹。 让Android知道在哪里可以找到您的图片。

2.添加ImageButton

打开“ res / layout / main.xml ”文件,添加“ ImageButton ”标签,并通过“ android:src ”定义背景图像。

文件:res / layout / main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><ImageButtonandroid:id="@+id/imageButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/android_button" /></LinearLayout>

3.代码代码

这是代码,在图像按钮上添加一个单击侦听器。

文件:MyAndroidAppActivity.java

package com.mkyong.android;import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;public class MyAndroidAppActivity extends Activity {ImageButton imageButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);addListenerOnButton();}public void addListenerOnButton() {imageButton = (ImageButton) findViewById(R.id.imageButton1);imageButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Toast.makeText(MyAndroidAppActivity.this,"ImageButton is clicked!", Toast.LENGTH_SHORT).show();}});}}

4.演示

运行应用程序。

1.结果是带有自定义背景图像的按钮。

2.单击按钮,显示一条短消息。

下载源代码

下载它– Android-ImageButton-Example.zip (28 KB)

参考文献

  1. Android ImageButton示例
标签: android imagebutton

翻译自: https://mkyong.com/android/android-imagebutton-example/


http://www.taodudu.cc/news/show-1990821.html

相关文章:

  • ImageView和ImageButton的区别
  • android+3.1.2+imagebutton监听,button以及Imagebutton的使用
  • WPF ImageButton
  • android imagebutton 动画,来自ImageButton的图像没有在Android Studio的模拟器上显示
  • android imagebutton 动画,android – ImageButton Icon Tint基于State
  • android imagebutton 点击效果缩小,ImageButton和ZoomButton使用
  • Flutter 自定义 ImageButton
  • Android 图像按钮ImageButton
  • 第9章、图像按钮ImageButton(从零开始学Android)
  • 用android编写使用按钮ImageButton和切换器ImageSwitcher
  • Android学习路之Button(按钮)与ImageButton(图像按钮)
  • 【Android 常见控件使用】Button(按钮)与ImageButton(图像按钮)
  • 本人psp2000和psp3000都亲自试验过,没问题,刷完可以关机,能读取ISO文件,具体某些游戏可能不能运行还需要自己实践。
  • PSP XREADER 1.63下载(PSP阅读器)v1.6最新版 含安装/使用方法
  • JAVA学习 第5天
  • PSP??? 生产者与消费者框架,还是线程模型?
  • Lumion文件结构及Benchmark in progrees问题
  • 2009年南京生活小结
  • 2009年南京辞职当日
  • psp2000 M33 自制固件---恢复模式说明(基本所有版本都适用)
  • [谈现在的PSP与NDSi]
  • PSP大神的退出
  • PSP3000/2000V3用5.03-GEN-C系统安装使用教程
  • PSP记忆棒文件放置结构图
  • Switch / Xbox / PS / PSV / PSP 游戏指南
  • 承载童年的游戏机,已停产!但我在 GitHub 找到了它们
  • 这可能才是PSV破解不了的真正原因—破解的无奈
  • PSARDumper DA发布PSP3000解密工具
  • PSP3000/2000V3用5.03GEN-C安装教程
  • 破解,汉化,越狱,解锁,为什么中国的大神总是“昙花一现”?

Android ImageButton示例相关推荐

  1. Android ImageButton示例代码

    1) XML File: activity_main 1)XML文件:activity_main <?xml version="1.0" encoding="utf ...

  2. android 工程搭建,Android ApiDemo示例工程的创建

    一般SDK中都会带有一些示例程序,说明具体用法,Android SDK也是如此.它提供了一些ApiDemo示例,详细说明了Android中主要API,分为以下几大类: 1.App 2.Content ...

  3. android jni示例_Android服务示例

    android jni示例 A service is a component that runs in the background for supporting different types of ...

  4. Android 经典示例,初学者的绝好源码资料

    2019独角兽企业重金招聘Python工程师标准>>> Android 经典示例,初学者的绝好源码资料 附上源码: 转载:http://www.adobex.com/android/ ...

  5. Android ListView示例教程

    We will learn how to create a simple Android ListView and launch a new activity on selecting a singl ...

  6. android jni示例_Android动画示例

    android jni示例 Android Animation is used to give the UI a rich look and feel. Animations in android a ...

  7. Android ActionBar示例教程

    Today we will look into Android ActionBar. Action Bar is one of the important part of any applicatio ...

  8. Android WebView示例教程

    Android WebView is used to display HTML in an android app. We can use android WebView to load HTML p ...

  9. Android GridView示例

    Android GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. In this t ...

  10. Android ViewPager示例教程

    ViewPager in Android allows the user to flip left and right through pages of data. In our android Vi ...

最新文章

  1. Android 自定义百分比视图
  2. 一个男人的潜力有多大,就看这几点!
  3. String , StringBuffer 和 StringBuilder 区别
  4. 不能不知道的Docker知识
  5. 香帅的北大金融学课笔记17 -- 公司治理
  6. mysql 存储过程 select 循环_简简单单储存过程——循环一个select结果集
  7. webpack 打包第三方库_webpack打包分离第三方库和业务代码
  8. Codeforces Round #642 (Div. 3)(AB)
  9. ASP.NET MVC:缓存功能的设计及问题
  10. STM32F1笔记(十三)SPI
  11. php在那个位置加载语言包,thinkphp 3.23语言包加载
  12. 【英语学习】【医学】Unit 03 Blood
  13. 团队编程项目作业1-成员简介及分工
  14. java 代码解析工具_改善 Java 代码质量的工具与方法
  15. log4j从1.x平滑升级至2.x
  16. Linux weget (文件 下载)安装方法
  17. 5393: YuYu的密码II
  18. 老王的JAVA基础课:序言
  19. linux shell 端口扫描,shell脚本结合iptables防端口扫描的实现
  20. sap中如何追踪生产订单的修改记录

热门文章

  1. Skyfree退休公告
  2. cad2010多个文件并排显示_CAD软件同时显示两张或多张图纸的方法
  3. 萤火虫(FA)算法(附完整Matlab代码,可直接复制)
  4. 四叶草Clover配置器:Clover Configurator for Mac
  5. Xshell 7免费版下载及安装
  6. mac word无法显示仿宋字体解决方案
  7. HTML网上书店静态HTML网页作业作品 大学生三联书店网页设计制作成品 简单DIV CSS布局网站
  8. 互联网+商业计划书(模版)
  9. SM4算法java代码实现
  10. Java的日期类说明Calendar、Data、日期转化格式化以及注意事项