【问题标题】:Android ListView delete row button - focus issueAndroid ListView删除行按钮 - 焦点问题
【发布时间】:2011-03-03 01:22:38
【问题描述】:

我有一个带有 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="fill_parent"
    android:orientation="vertical">

    <ListView android:id="@+id/lvLamps" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:listSelector="@null"
        android:choiceMode="none" android:scrollbarStyle="insideInset"
        android:layout_weight="1.0" />

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_weight="0.0">

        <Button android:id="@+id/btnAdd" android:background="@null"
            android:drawableLeft="@drawable/btn_upgrade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/lbl_upgrade"
            android:textSize="0pt" android:text=""
            android:layout_alignParentLeft="true" android:padding="20px" />


        <Button android:id="@+id/btnNext" android:background="@null"
            android:drawableRight="@drawable/next_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/lbl_next"
            android:textSize="0pt" android:text=""
            android:layout_alignParentRight="true" android:padding="20px"
            android:visibility="gone" />

        <ImageButton android:id="@+id/btnListExit"
            android:background="@null" android:src="@drawable/btn_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" android:padding="20px" />

    </RelativeLayout>
</LinearLayout>

ListView 行包含删除按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal" android:focusable="true">
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" android:focusable="true">
        <ImageButton android:id="@+id/btnRowDelete" 
            android:src="@drawable/btn_x"
            android:background="@null" android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" android:padding="4px"  
            android:focusable="true" android:focusableInTouchMode="true"/>
            <TextView android:id="@+id/txtLampRowFrom" android:text="123"
                android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" android:textSize="6pt"
                    android:layout_toRightOf="@id/btnRowDelete"
                    android:focusable="false"
                    android:textColor="@color/textColor"/>
            <TextView android:id="@+id/txtLampRowTo" android:text="123"
                android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                android:textSize="6pt"
                    android:layout_toRightOf="@id/btnRowDelete"
                    android:layout_alignParentBottom="true"
                    android:focusable="false"
                    android:textColor="@color/textColor"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_upgrade_to"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" android:focusable="false"/>
    </RelativeLayout>
</LinearLayout>

在Adapter中,设置了按钮onClickListener,也有假人使列表不可选:

    // disabling list items select
    public boolean areAllItemsEnabled() {
        return false;
    }

    public boolean isEnabled(int position) {
        return false;
    }

我想要的是:

  • 列表后总是在屏幕底部显示按钮(不管多长,如果太长应该有滚动条)
  • ListView 不应该是可选择的,我不想要行选择
  • 行删除按钮应该可以通过触摸和轨迹球选择(聚焦)

一切正常,除了我无法聚焦行删除按钮使用轨迹球(虽然它可以使用触摸)。你能帮助我吗?

谢谢!

【问题讨论】:

    标签: android user-interface listview button focus


    【解决方案1】:

    使用

    listView.setItemsCanFocus(true);
    

    【讨论】:

    • 我相信您的按钮可以通过上面的代码获得焦点,但您只是看不到它。尝试使用常规按钮而不是 ImageButton,您将看到按钮可以聚焦。如果要使用 ImageButton,则需要指定按钮在获得焦点时的外观。当按钮获得焦点时,您将希望为按钮显示不同的图像。为此,您需要使用选择器。检查此链接以了解如何编写选择器 androidwizard.net/2010/02/… 抱歉,迟到的答案... ;)
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多