安卓:ListView控件的按钮 - > OnItemClick做什么做什么、控件、按钮、ListView

2023-09-06 03:35:11 作者:宅男费纸

我的问题来自于ListView控件。如果没有按钮,在 .setOnItemClickListener 在ListView工作做好的。 (开始活动)

My problem come from ListView. Without Buttons, the .setOnItemClickListener of the ListView work well. (start an Activity)

在时间,我将按钮添加到ListView中的项目,按钮是可以点击的,但项目并不多点击...

At time that I add Buttons into items of the ListView, Buttons are clickable but the items aren't more clickable...

我尝试用 myListView.setItemCanFocus(真); ,但它不工作......

I try to use myListView.setItemCanFocus(true); but it don't work...

推荐答案

从按钮删除可获得焦点属性会解决这个问题。你可以做到这一点无论是在布局XML文件或Java源代码code。

Remove the focusable attribute from the Button would solve this problem. You could do that either in a layout xml file or java source code.

和另外一个技巧,如果你正在使用的不是按钮的ImageButton,你需要setFocusable在Java code,使这项工作,导致的ImageButton的构造函数enabe这个属性的XML文件后膨胀。

And one more tip, if you are using ImageButton instead of Button, you need setFocusable in your java code to make that work, cause the constructor of ImageButton would enabe this attribute after inflate from xml file.