Android LinearLayout 上的 onClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2130875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:39:38  来源:igfitidea点击:

onClickListener on a LinearLayout

android

提问by Maxim Yudin

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

我想实现的功能有点问题。

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

我有一个LinearLayout,包含 2 TextViews。我想onClickListener在它上面设置 a ,这样它的行为就像ListView.

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

我已经尝试了一些可能的实现这一点,并得到它的工作使用XML属性android:onClickLinearLayout

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 :

由于我希望应用程序对单击LinearLayout(如 a ListView)做出直观反应,因此我将其放入侦听器回调方法中:

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

l is a LinearLayoutobject, initialised with findViewById();

l 是一个LinearLayout对象,初始化为findViewById();

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.

每当我尝试单击布局时,应用程序都没有响应。我试图通过 Toast 消息显示替换回调方法中的代码,并且它有效,所以我猜问题出在setBackgroundColor.

Is anyone familiar with this?

有没有人熟悉这个?

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

PS:抱歉,我现在不能发布更多代码,我正在工作。

回答by Maxim Yudin

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

您必须在 xml 布局文件或java 活动代码中设置LinearLayout属性。android:clickable="true"setClickable(true)

回答by richard

i noticed that all the advices above don't helpt as long as any of the child elements inside the LinearLayout has the attribute android:textIsSelectable="true".

我注意到,只要 LinearLayout 中的任何子元素具有 android:textIsSelectable="true" 属性,上面的所有建议都没有帮助。

回答by Glenn

I found that that setClickable(true) would still cause clicks to go to children of the linearlayout. I found that to have the LinearLayout capture the touch instead of it's children I had to override the dispatchTouchEvent method so I created a subclass of LinearLayout for just this purpose. Seems like an ugly solution though.

我发现 setClickable(true) 仍然会导致点击转到线性布局的子项。我发现要让 LinearLayout 捕获触摸而不是它的子类,我必须覆盖 dispatchTouchEvent 方法,因此我为此创建了 LinearLayout 的子类。虽然看起来像一个丑陋的解决方案。

回答by facebook-bszbrf

you should set the LinearLayout's focusable to true and set all the children view's focusable to false, don't use the android:clickable="true", but you can't see the effect of the click of the linelayout. BTW, the best way is to implement the onTouchEvent api.

你应该将LinearLayout的focusable设置为true,将所有子视图的focusable设置为false,不要使用android:clickable="true",但是你看不到linelayout的点击效果。顺便说一句,最好的方法是实现 onTouchEvent api。

回答by Jjang

Had the same problem, I've been trying for an hour all the answers in SO but none worked.

有同样的问题,我已经尝试了一个小时的所有答案,但没有一个奏效。

Then I realized I just had to promote my LinearLayout to the bottom of the layout, since other views blocked it, and then it worked, without adding ANY special attributes to the layout.

然后我意识到我只需要将我的 LinearLayout 提升到布局的底部,因为其他视图阻止了它,然后它就可以工作了,而无需向布局添加任何特殊属性

Putting it here in case it might help someone someday.

把它放在这里,以防有一天它可能对某人有所帮助。