如何将TextView设置为onClick的情况?安卓
因此,我在活动中声明了一个 TextView 变量,例如
TextView t;
The Activity Implements an OnClickListener so it has the function
public void onClick(View v){
//switch (v.getId()) {
//case:.......
}
我知道您可以在 switch 语句中使用 R.id.itemID 来告诉某些项目在 onClick 上执行某些操作,但是,就我而言:
1)我正在使用带有 viewswitcher.viewfactory 的文本切换器
2)由于上述原因,在我的主 xml 布局中,
<TextSwitcher> without a surrounding
<TextView> so the only id I have is the textswitcher
当我单击我所做的文本视图的文本时,我使用它来执行某些操作
t.setOnClickListener(this)
但是,我不知道在 switch 语句中将 case 设置为什么,所以我只使用默认值,如果我向侦听器设置其他视图和内容,这当然可能会导致一些大问题。
那么有没有另一种方法可以在没有 xml id 的情况下在 textview 上设置 onclicklistner 或者有没有办法找出 textview 的 id ?
谢谢!
So I have a TextView variable declared in the activity such as
TextView t;
The activity implements an OnClickListener so it has the function
public void onClick(View v){
//switch (v.getId()) {
//case:.......
}
I know you can use R.id.itemID in the switch statement to tell some item to do something onClick, however, in my case:
1) I'm using a textswitcher with viewswitcher.viewfactory
2) because of the above, in my main xml layout, i'm using
<TextSwitcher> without a surrounding
<TextView> so the only id I have is the textswitcher
so to do something when I click the text of the textview I did
t.setOnClickListener(this)
However, I don't know what to set the case in the switch statement to, so I just used default, which of course could cause some big problems if I set other views and stuff to the listener.
So is there another way to set the onclicklistner on textview without an xml id or is there a way to figure out the id of the textview?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做 t.setTag("yourTag") (参见 http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object) 然后在 if 语句中执行 v.getTag 而不是 switch 语句,因为它或者
,我认为您可以在资源中定义一个唯一的 id (请参阅 http://developer.android.com/guide/topics/resources/more-resources.html#Id") 然后使用 setTag(int key, tag) http://developer.android.com/reference/android/view/View.html#setTag (int, java.lang.Object)
我经常看到人们只是设置一个标签,然后稍后引用它。
you could do t.setTag("yourTag") (see http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object) and then do v.getTag in if statements rather than a switch statement since it will be a string.
or alternatively, I think you can define a unique id in your resources (see http://developer.android.com/guide/topics/resources/more-resources.html#Id") then use setTag(int key, tag) http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object)
i've mostly seen people just set a tag and then reference that later.
好吧,即使您使用 ViewSwitcher,也没有理由不能在 XML 文件中的 TextView 上设置 id。
但是,如果您坚持不在 xml 中设置 id,那么您可以这样做:
并且您将拥有一个可以在 switch 语句中使用的 id。
Well, even if you are using a ViewSwitcher, there is no reason why you can't set an id on the TextView in the XML file.
However, if you insist on not setting an id in xml, then you can do this:
and you'll have an id you can use in your switch statement.