onClickListener上的LinearLayoutonClickListener、LinearLayout

由网友(Review(旧爱))分享简介:得到了一个功能一个小问题,我想实现。Got a little problem with a functionality I'd like to implement.我有一个的LinearLayout ,含2 TextViews 。我想设置一个 onClickListener 就可以了,因此,这将表现就像一个的Li...

得到了一个功能一个小问题,我想实现。

Got a little problem with a functionality I'd like to implement.

我有一个的LinearLayout ,含2 TextViews 。我想设置一个 onClickListener 就可以了,因此,这将表现就像一个的ListView项目

I've got a LinearLayout, containing 2 TextViews. I'd want to set a onClickListener on it, so that it would behave just like an item in a ListView.

我已经尝试了一些可能实现这一点,并把它工作使用XML属性安卓的的LinearLayout 。

I've tried some possible implementation of this, and got it working using the xml attribute android:onClick of the LinearLayout.

正如我希望应用程序在视觉上的反应,点击的的LinearLayout (如在的ListView ),我把这个监听器回调方法:

As I want the application to visually react to a click on the LinearLayout (as in a ListView), I put this in the listener callback method :

l.setBackgroundColor(android.R.drawable.list_selector_background);

L是的LinearLayout 对象,初始化与 findViewById();

l is a LinearLayout object, initialised with findViewById();

我得到一个应用程序没有响应,每当我尝试点击布局。我试图通过敬酒消息来替换code回调方法展现出来,和它的作品,所以我想这个问题是在 setBackgroundColor

I get an application not responding whenever I try to click on the layout. I tried to replace the code in the callback method by a Toast message show up, and it works, so I guess the problem is in the setBackgroundColor.

是任何人都熟悉这个?的

PS:很抱歉,我不能发布更多的code,现在,我在工作

PS: sorry, I can't post more code right now, I'm at work.

推荐答案

您必须设置的LinearLayout 属性机器人:可点击=真正的在XML布局文件或 setClickable(真)在java活动code。

You have to set LinearLayout attribute android:clickable="true" in the xml layout file or setClickable(true) in the java activity code.

    Copyright 2016-2020 新思维的学习:onClickListener上的LinearLayoutonClickListener、LinearLayout、All Right Reserved 浙ICP备20029812号-1