TextView 点击事件无效的完美解决

来源:互联网 发布:360浏览器mac版 编辑:程序博客网 时间:2024/05/24 15:38
最近在开发中遇到这么一个问题,当使用TextView做成Button的点击事件时(在xml中使用android:onClick="click"属性设置该TextView的点击方法),点击失去效果,不能触发点击事件。


在xml中是这么写的:

    <TextView        android:id="@+id/tv_click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="点击" />


后来查了资料发现这是因为国产手机的系统进行过修改,在一些机型上TextView的 android:clickable默认值为false,所以如果要在xml中写TextView的点击事件回调的话,一定要写上android:clickable="true";而Button、ImageButton这种本身就是点击类的控件则默认android:clickable="true"所以以上代码最好写成可点击的状态

    <TextView        android:id="@+id/tv_click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="点击" />


谢谢!!!!