如何点击或点击一个TextView文本 [英] How to click or tap on a TextView text
问题描述
我知道这是很容易(DOH ...),但我在寻找一种方式来运行上点击或单击文本的一个TextView线的Android应用程序的方法。
I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
我一直在想着按钮听众和匿名方法的侦听器调用,但它只是似乎并不适用于TextView的。
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
有人点我一些code片段来展示如何点击或点击上一段文字中一个TextView运行的方法?
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
推荐答案
您可以使用这些属性设置单击处理程序中的XML:
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
不要忘记点击属性,没有它,单击处理程序没有被调用。
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml中
...
<TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
这篇关于如何点击或点击一个TextView文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!