Android listview中的按钮
我有行布局的listview,如Android listview中的按钮,android,android-listview,Android,Android Listview,我有行布局的listview,如 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="horizon
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/ArriveTime"
android:gravity="center"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Name"
android:gravity="center"
android:layout_weight="1" />
<Button
android:focusable="false"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Arrive"
android:gravity="center"
android:layout_weight="1"
android:text="Arrive" />
<Button
android:focusable="false"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Encounter"
android:gravity="center"
android:layout_weight="1"
android:text="Encounter" />
<Button
android:focusable="false"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Exit"
android:gravity="center"
android:layout_weight="1"
android:text="Exit" />
</LinearLayout>
问题是,在第一次单击该行后,按钮将处于活动状态,我无法再次单击该行。要解决此问题,我需要激活该行上的按钮,我单击该行,然后将焦点转移到该行,以便进一步单击,除setFocusable(true)外,您还可以使用setChecked(布尔b)。但您可以在CompoundButton上使用此方法
来自java:
CompoundButton cb = (CompoundButton) findViewById(R.id.yourButton);
cb.setChecked(true);
在XML中
<CompoundButton
android:id="@+id/yourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_stats"
android:text="@string/stat_hard">
</CompoundButton>
其中button_stats.xml是位于res/drawable中的文件,其中包含以下行:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/generic_button_normal" android:state_checked="false"/>
<item android:drawable="@drawable/generic_button_checked" android:state_checked="true"/>
</selector>
您可以尝试使用:
android:descendantFocusability="blocksDescendants"
在布局XML中,而不是单独修改每个元素
您可以在listview中阅读有关按钮的更多信息
特别是评论#27和#31
希望它能帮助你如果你想让线条和按钮都可以(一直)点击,你不需要经历所有这些旋转。只需将这两行添加到xml行中的按钮标注:
android:focusable="false"
android:focusableInTouchMode="false"
然后,您只需在适配器中为每个按钮设置button click侦听器,并在您正常调用列表的活动中设置项click侦听器
如果您希望在单击该行之前禁用按钮(我不清楚这是目标还是您试图实现两个按钮均可单击的方式的后遗症),xml中的设置与可聚焦标注相同,将按钮设置为禁用,然后在
onListItemClick
中使用标志设置if
语句,通过使用按钮将其切换为可单击和不可单击。setEnabled(false)代码>和按钮。设置启用(true)代码>如果radiobutton小部件适合您的用途,您可以使用它。
android:focusable="false"
android:focusableInTouchMode="false"