記錄Android開發一個小坑,佈局文件TextView中添加onClick後,點擊無效問題

自己寫東西的時候,在TextView上添加了onClick去增加點擊事件,去跳轉另一個Activity,運行後結果點擊無效,添加Toast,Toast也不顯示,代碼如下:

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content
        android:text="Hello World!"
        android:onClick="toSecond"/>

在網上找了一下 ,發現TextView默認clickable是false的,所以在上面代碼中加一句android:clickable="true"就可以了:

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content
        android:text="Hello World!"
        android:onClick="toSecond"
        android:clickable="true"/>

這樣,TextView使用onClick處理點擊事件就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章