安卓开发中,布局文件中的控件有一个属性,是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. 对一个表中所有列数据模糊查询adoquery

    如何用adoquery对一个表中所有列进行模糊查询: procedure TForm3.Button4Click(Sender: TObject); var ASql,AKey: string; I: ...

  2. 解决yum命令时出现Error: xz compression not available

    由于CentOS6的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compression not available问题. ...

  3. VS2010编译以前版本工程时 ERROR CVT1100:duplicate resource,type:MANIFEST解决办法

    1.将 Resource Files 里面的 *.exe.manifest 文件删除 2.右键选择 *.rc 文件,选择 view code,查找并删除所有引用 *.exe.manifest 文件的代 ...

  4. SharePoint 2013 配置InfoPath 列表表单

    转载来源:http://www.cnblogs.com/jianyus/p/3470113.html SharePoint列表,都是通过表单展示,有时候不太符合要求,这时候,我们可以通过定制表单,来是 ...

  5. 启动tomcat时报错:java.lang.OutOfMemoryError: PermGen space

    1.修改myeclipse.ini 在Myeclipse安装目录下G:\MyEclipse8.5\Genuitec\MyEclipse 8.5有一个myeclipse.ini配置文件,设置如下: -v ...

  6. 移动UI

    UI设计需要关注:色彩,信息布局,交互流程,用户体验,尺寸等

  7. 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本

    Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...

  8. iOS可变字符串的所有操作

    可直接把代码复制即可结合输出看结果加深记忆 //创建一个可变字符串 NSMutableString * ms1 = [[NSMutableString alloc]init]; //可以通过类方法来创 ...

  9. C语言_cmd_pause

    再C语言里面使用system函数调用pause. system("pause"); 会显示   请按任意键继续. . . system("pause ->nul&q ...

  10. selenium C#下的zencart自动化测试(WFloginUrlPayment)环境4.0

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...