Android:如何将“视图”转换为“视图”到“文本视图”?
我有许多 TextView
,每个都共享一个 OnLongClickListener
在 onLongClick
事件中,我想识别哪个 TextView 触发了该事件。
但是,该事件定义为:
public boolean onLongClick(View view)
我尝试将 view
转换为 TextView
,但这不起作用。
如何获取触发 OnLongClick
事件的小部件?
I have a number of TextView
s that each share a single OnLongClickListener
Within the onLongClick
event, I want to identify which TextView
triggered the event.
However, the event is defined as:
public boolean onLongClick(View view)
I tried casting view
to TextView
, but that didn't work.
How can I get at the widget that triggered the OnLongClick
event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
View
应该是您的 TextView。尝试这样的操作:
要验证上面的 if 语句是否有效,您可以尝试先像这样运行它:
The
View
should be your TextView.Try something like this:
To verify that the above if-statement is valid you can try running it like this first:
使用 view.getId() 方法获取单击视图的 id。
Use view.getId() method to get id of clicked view.
TextView
已经是一个View
,所有的小部件都从View
扩展,所以你需要的只是一个开关在 TextView 的id
上,如下所示:The
TextView
is Already aView
, All Widgets Are extending fromView
, so all what you need is a switch on theid
of your TextViews like this :你得到的错误是什么?通常这应该有效。如果您设置了
yourTextView.setOnLongClickListener(this)
,则public boolean onLongClick(View view)
将触发,您无需强制转换它。what's the error you are getting ? normally this should work. If you set the
yourTextView.setOnLongClickListener(this)
, and thenpublic boolean onLongClick(View view)
will trigger and you don't need to cast it.