1

我有行布局的列表视图,如

<?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>

我希望点击行时能够点击按钮,所以我设置android:focusable="false"并点击我说

    MainGrid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

             Log.d("zzzzzzzzzzzzzzzzzzzzzzzz",""+ oldPosition);

            if(oldPosition != -1)
            {
                MainGrid.getChildAt(oldPosition).setFocusable(false);
                //MainGrid.findViewById(oldPosition).setFocusable(false);
            } 

            // Set the whole list view unfocusable 
             MainGrid.setFocusable(false);


            // focus only on this 
            view.setFocusable(true);

            oldPosition = position -1;

            SetMenueOnClick(position-1) ;
 }});

问题是,在第一次单击该行后,按钮处于活动状态,我无法再次单击它任何解决此问题的想法,我需要激活我单击的行上的按钮,然后将焦点转移到该行以进一步单击

4

3 回答 3

3

如果您希望线条和按钮都可点击(一直),您不需要经历所有这些旋转。只需将这两行添加到 xml 行中的按钮标注中:

    android:focusable="false"
    android:focusableInTouchMode="false"

然后,您只需在适配器中为每个按钮设置按钮单击侦听器,并在您正常调用列表的活动中设置项目单击侦听器。

如果您希望在单击该行之前禁用按钮(我不清楚这是目标还是只是您试图让两者都可点击的方式的后果),xml中的设置是与可聚焦标注相同,将按钮设置为禁用,然后在onListItemClick设置if语句中使用标志来切换它们可点击和不可点击使用button.setEnabled(false);and button.setEnabled(true);

于 2012-06-12T14:34:13.023 回答
0

除了 setFocusable(true),您还可以使用 setChecked(boolean 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>
于 2012-06-12T14:31:28.000 回答
0

您可以尝试使用:

android:descendantFocusability="blocksDescendants"

在您的布局 XML 中,而不是单独修改每个元素。

您可以在列表视图问题中阅读有关按钮的更多信息here

特别是评论 #27 & #31

希望能帮助到你

于 2012-06-12T14:33:32.963 回答