Android:如何将“视图”转换为“视图”到“文本视图”?

发布于 2024-12-17 09:47:52 字数 359 浏览 4 评论 0原文

我有许多 TextView,每个都共享一个 OnLongClickListener

onLongClick 事件中,我想识别哪个 TextView 触发了该事件。

但是,该事件定义为:

public boolean onLongClick(View view) 

我尝试将 view 转换为 TextView,但这不起作用。

如何获取触发 OnLongClick 事件的小部件?

I have a number of TextViews 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我纯我任性 2024-12-24 09:47:53

View 应该是您的 TextView。

尝试这样的操作:

if( view instanceof TextView ) {
  TextView textView = (TextView) view;
  //Do your stuff
}

要验证上面的 if 语句是否有效,您可以尝试先像这样运行它:

if( view instanceof TextView ) {
  Log.e( "MyTag", "It's a TextView!" );
}

The View should be your TextView.

Try something like this:

if( view instanceof TextView ) {
  TextView textView = (TextView) view;
  //Do your stuff
}

To verify that the above if-statement is valid you can try running it like this first:

if( view instanceof TextView ) {
  Log.e( "MyTag", "It's a TextView!" );
}
嘦怹 2024-12-24 09:47:53

使用 view.getId() 方法获取单击视图的 id。

Use view.getId() method to get id of clicked view.

神经大条 2024-12-24 09:47:53
public boolean onLongClick(View v) {
        switch(v.getId()) {
            case R.id.first_text_view: // do things here; break;
                ...
        }
        return true;
    }
public boolean onLongClick(View v) {
        switch(v.getId()) {
            case R.id.first_text_view: // do things here; break;
                ...
        }
        return true;
    }
瘫痪情歌 2024-12-24 09:47:53

TextView 已经是一个View,所有的小部件都从View扩展,所以你需要的只是一个开关在 TextView 的 id 上,如下所示:

public boolean onLongClick(View v) {
        switch(v.getId()) {
            case R.id.txt1: // your code for the textView which have the id R.id.txt1  ...;
                     break;
            case R.id.txt2: // your code for the textView which have the id R.id.txt2  ...; 
                    break;
                //... etc
        }
        return true;
    }

The TextView is Already a View, All Widgets Are extending from View, so all what you need is a switch on the id of your TextViews like this :

public boolean onLongClick(View v) {
        switch(v.getId()) {
            case R.id.txt1: // your code for the textView which have the id R.id.txt1  ...;
                     break;
            case R.id.txt2: // your code for the textView which have the id R.id.txt2  ...; 
                    break;
                //... etc
        }
        return true;
    }
断舍离 2024-12-24 09:47:53

你得到的错误是什么?通常这应该有效。如果您设置了 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 then public boolean onLongClick(View view) will trigger and you don't need to cast it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文