安卓开发中,布局文件中的控件有一个属性,是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. 单击dbgrid列标题排序 升降序

    delphi中如何通过单击列标题进行升降排序, 在dbgrid的ontitleclick事件里添加这样的事件处理 procedure TForm3.DBGrid1TitleClick(Column: ...

  2. jquery 动态生成元素 事件

    $(document).on("click",".detail",function () {});

  3. UITextField的属性设置

    1.背景颜色 field.backgoundColor = [UIColor redColor]; 2.设置field文字 field.text = @"输入文字"; 3.设置fi ...

  4. UIButton的属性设置

    1.背景颜色 btn.backgroundColor = [UIColor redColor]; 2.给按钮添加文字并添加显示状态  [btn setTitle@"播放" forS ...

  5. ECOS 系统查找商品详情图片存入mysql情况。

    SELECT g.goods_id, g.bn,g.name,b.brand_name,g.price,g.mktprice,c.cat_name into outfile '/tmp/xxx.xls ...

  6. JS 经典代码段总结 start from 2016-08-22

    1.for(var i = 0, max = myArray.length; i < max ; i++){ //用myArrayy[i]来做点什么 } 用max存储myArray的长度,防止每 ...

  7. GetWindowRect和GetClientRect的异同

    由于项目需要,需要学习CGridCtrl控件的使用,测试控件时发现了一个问题,我无法将控件放在对话框的制定位置. 该问题的原因很容易发现,其实就是GetWindowRec()函数和GetClientR ...

  8. HTML中加载flash方法

    首先贴上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  9. android 实践项目

    电子词典  http://files.cnblogs.com/blogLYF/lyf_danci.apk

  10. iOS模拟器沙盒使用推荐

    iOS沙盒路径的查看和使用 1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library.因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制 ...