使用相同的onClick监听器与多个视图 [英] Using same onClick listener with more than one view

查看:126
本文介绍了使用相同的onClick监听器与多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相同的onClick侦听器的一些项目。

I am using the same onClick listener for a number of items.

当我点击我想知道是哪一个。

When I click I want to know which one.

我知道我可以做一个switch语句上的getId(),但宁可能够得到在项目的名称。
有没有简单的方法来做到这一点?

I know that I can do a Switch statement on the getId() but would rather be able to get at the name of the item. Is there any easy way to do this?

推荐答案

我想你是指当你说得到名是从资源的ID字符串到什么。所以,你会喜欢有一个switch语句:

I think what you are referring to when you say "get the name" is the id string from resources. So you would have a switch statement like:

switch(view.getId()) {
    case R.id.HomeButtonOne:
        // Do Button One Action
        break;
    case R.id.HomeButtonTwo:
        // Do Button Two Action
        break;
}

否则请更多地讨论你想达到什么目的。

otherwise please elaborate more on what you are trying to achieve.

这篇关于使用相同的onClick监听器与多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆