Android的-List查看含有不再选择按钮和TextView中 [英] Android -List View containing buttons and textview no longer selectable

查看:232
本文介绍了Android的-List查看含有不再选择按钮和TextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过这个网站有关按钮和文本视图不能够ListView中的许多职位被选中。但我的问题是不同的,我不能够在与其他相关的帖子的结论得出。
我有一个BaseAdapter填充列表视图。列表视图的布局是在左边2 textviews并在每一行权的按钮。我希望按钮以及列表视图的整行选择能。

I have been through many posts on this site about listview with buttons and text view not being able to be selected. But my problem is different and I'm not able to derive at a conclusion from other related posts. I have a listview populated by a BaseAdapter. The list view's layout is 2 textviews on the left and a button on the right for each row. I want the button as well as entire row of the listview to be able to selected.

我知道按钮在使用得到焦点时。谁能告诉我怎样才能让这两个按钮以及整个行 - 可选择

I know the button when used is gaining focus. Can someone tell me how can i make both button as well as entire row - selectable?

问候,
阿吉特

Regards, Ajith

推荐答案

我面临同样的问题也和我如何解决它在Android API级别16.最重要的是具有<下面的示例code STRONG>机器人:可点击和机器人:可聚焦设置为true

I faced the same problem too, and below sample code on how I solved it on Android API level 16. The most important thing is having the android:clickable and android:focusable set to true.

在你行的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/list_selector_background"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

在你的基地适配器,您可以再设置监听器做一些事情:

In your base adapter, you can then set the listener to do something:

public class MyBaseAdapter extends BaseAdapter {

    // Some other method implementation here...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Initialize the convertView here...

        LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.row_layout);
        Button button = (Button) convertView.findViewById(R.id.button);

        layout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "Row clicked!", Toast.LENGTH_LONG).show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "Button clicked!", Toast.LENGTH_LONG).show();
            }
        });
    }

}

这篇关于Android的-List查看含有不再选择按钮和TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆