java Android Row 变成不可点击的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2322390/
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
Android Row becomes Unclickable with Button
提问by Kleptine
I have a listView, where each row has a button in the row layout. However, this seems to make the row itself unclickable. How can I make both the button and row clickable?
我有一个 listView,其中每一行在行布局中都有一个按钮。但是,这似乎使行本身无法点击。如何使按钮和行都可点击?
Thanks.
谢谢。
回答by jqpubliq
You need to set itemsCanFocus on the list like this:
您需要像这样在列表上设置 itemsCanFocus:
mList.setItemsCanFocus(true);
To make the button clickable. Then you will need to use your own adapter and in getView return a view which is Clickable and focusable. You will also lose the default highlight states so you need to put them back in with the background resource. So do this:
使按钮可点击。然后你需要使用你自己的适配器并在 getView 中返回一个可点击和可聚焦的视图。您还将丢失默认的高亮状态,因此您需要将它们与背景资源一起放回原处。所以这样做:
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
to your view before returning your view.
在返回您的视图之前转到您的视图。
回答by Matteo Danieli
Whenever I see posts concerning the android:focusableand android:clickableattributes, I always see them being both set to the same value at once. I figured there must be a reason if they are two separate attributes instead of being one.
每当我看到有关android:focusable和android:clickable属性的帖子时,我总是看到它们同时被设置为相同的值。我认为如果它们是两个独立的属性而不是一个属性,那一定是有原因的。
It turns out that a much better way of achieving your desired behavior is to set
事实证明,实现所需行为的更好方法是设置
android:focusable="false"
or
或者
yourButton.setFocusable(false)
on the Buttonin your View. Once you do that, you'll be both able to set an OnClickListeneron the Button, and a click on the row will fire the onListItemClick()method in your OnItemClickListener.
就Button在你的View。一旦你这样做,你既可以设置OnClickListener的Button,并在该行一点击就会触发该onListItemClick()方法在你的OnItemClickListener。
回答by βhargav?
Try to set your widgets to non clickable and non focusablein xml,the click on items will work normally and also the click on button will work normally.
尝试在 xml中将您的小部件设置为不可点击和不可聚焦,点击项目将正常工作,点击按钮也将正常工作。
android:clickable="false"
android:focusable="false"
Hope this helps.
希望这可以帮助。

