在Android的可点击的TextView [英] Clickable TextView in Android
问题描述
我要建一个Android应用程序,有我想可以点击很多很多TextViews。
我试着分配的属性机器人:可点击=真正的
和安卓的onClick =clickHandler事件
来单个的TextView
键,当应用程序触发 clickHandler事件(查看V)
我正确地通过<$ C获得点击的项目$ C> v.getId()。
我不喜欢的是定义,每一个的TextView
属性机器人:可点击
和安卓的onClick
......是有什么,我可以用code做地说:所有的textviews是可以点击的点击 clickHandler事件处理code>?
I'm building an Android App that has many many TextViews that I want to be clickable.
I've tried to assign the properties android:clickable="true"
and android:onClick="clickHandler"
to the single TextView
and when the app fires the clickHandler(View v)
I get the clicked item correctly through v.getId()
.
What I don't like is to define, for every TextView
, the properties android:clickable
and android:onClick
... is there something that i can do by code to say: "all the textviews are clickable and click is handled in clickHandler
?"
感谢。
推荐答案
您可以做这样的事情如下 - 这种方式,他们都具有相同的处理程序:
You could do something like this below - this way they all have the same handler:
public class sticks extends Activity implements View.OnTouchListener {
private TextView tv1;
private TextView tv2;
private TextView tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
// bind listeners
tv1.setOnTouchListener(this);
tv2.setOnTouchListener(this);
tv3.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// check which textview it is and do what you need to do
// return true if you don't want it handled by any other touch/click events after this
return true;
}
}
这篇关于在Android的可点击的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!