安卓开发中,布局文件中的控件有一个属性,是onClick,例如:

 
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onButtonClick"
            android:text="Button" />

在相应的Activity中实现onButtonClick方法,就可以实现Button绑定点击监听事件,非常简单方便。

 
    public void onButtonClick(View view){
     Toast.makeText(MainActivity.this, view.getId()+"", Toast.LENGTH_LONG).show();
    }
 
但是,在TextView中,这个设置无效:
 
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onButtonClick"
            android:text="Large Text"/>
 
解决办法,添加android:clickable="true"
 
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="onButtonClick"
            android:text="Large Text"/>
不同的控件默认的android:clickable属性值是不同的,所以,在设置Button以外的View的android:onClick="onButtonClick"时,最好,加上android:clickable="true"。

Android中布局文件中使用onClick属性的更多相关文章

  1. android中布局文件中 layout_weight 的属性详解

    在不同的情况下,layout_weight属性作用是不同的.主要有两种属性: 1.当布局中的控件的尺寸(宽和高)都有指定时,它所表示的该控件在父容器中的比重,及它在父容器中所占的比例,数值越大,比重越 ...

  2. android xml布局文件中tools:layout的作用

    摘要 用最新版本的adt 创建一个基于master/detail flow 模版的app的时候,生成的 activity_item_list.xml 文件中有一个tools:layout属性: fra ...

  3. android xml 布局文件中 android:ems="10"

    宽度为10个字符的宽度 xml中 android:ems属性 ,作为EditText 默认生成 的属性,其含义是需要编辑的 字符串长度 .设置为10时,最多编辑 10个em ,一个em单位是 两个in ...

  4. Android 自定义View及其在布局文件中的使用示例

    前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...

  5. android 布局文件中xmlns:android="http://schemas.android.com/apk/res/android"

    http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://sch ...

  6. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  7. Android 自定义View及其在布局文件中的使用示例(二)

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...

  8. Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?

    Android布局文件中的"@+id"和"@id"有什么区别? +id表示为控件指定一个id(新增一个id),如: <cn.codingblock.vie ...

  9. 在布局文件中使用Fragment的步骤

    为了在Activity布局文件中使用Fragment我们需要四个步骤. 1.定义一个Activity,他继承android.support.v4.app.FragmentActivity,下面是关键代 ...

随机推荐

  1. 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  2. 丢掉鼠标-Mac神软Alfred使用手册

    上篇: http://wellsnake.com/jekyll/update/2014/06/15/001/?utm_source=tuicool 下篇: http://wellsnake.com/j ...

  3. PDO对象

    <?php //造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;host=localhost"; //造P ...

  4. iOS开发之 用第三方类库实现ScrollView

    转自:http://www.cnblogs.com/qianLL/p/5369127.html   在github上面有很多的第三方类库,大大节约了大家的开发时间 下载地址:https://githu ...

  5. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  6. 正则表达式判断ip地址

    html: <div class="configuration"><form action="" name="myformcon&q ...

  7. java代理的深入浅出(一)-Proxy

    java代理的深入浅出(一)-Proxy 1.什么是代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事 ...

  8. javaWEB总结(6):ServletRequest

    1.首先看ServletRequest的API javax.servlet Interface ServletRequest All Known Subinterfaces: HttpServletR ...

  9. 曾经的ps的图片

  10. hdu_5680_zxa and set(想法题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5680 题意: 问题描述 zxa有一个集合A=\{a_1,a_2,\cdots,a_n\}A={a​1​ ...