如何在ListView标题中使按钮可单击

时间:2013-11-05 08:39:37

标签: android listview android-listview layout-inflater

我在ListView标题中有两个按钮,我想从标题中点击检测按钮。我怎样才能做到这一点.. 这是我的代码:

header_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2" />
</LinearLayout>

在java代码中我已经完成了这个

LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);

2 个答案:

答案 0 :(得分:3)

您可以为这些按钮添加侦听器。

btnB1 = (Button) mTop.findViewById(R.id.b1);
btnB1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Your code.

        }
    })

答案 1 :(得分:0)

LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
Button _btnb1 = (Button) mTop.findViewById(R.id.b1);
_btnb1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // Your code.

    }
});

Button _btnb2 = (Button) mTop.findViewById(R.id.b2);
_btnb2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // Your code.

    }
});

试试此代码

相关问题