自定义视图中的OnClickListener未被触发

14

我创建了一个自定义按钮,它是从XML布局中膨胀出来的。除了点击监听器没有被触发之外,一切正常。
我怀疑问题在于 android:clickable="true" 属性,因为当我将其移除时,点击监听器就会被触发。但是我需要将这个属性设置为 true,因为我的自定义视图使用选择器作为背景,如果我移除它,那么选择器就不再起作用了。

以下是类定义:

public class CustomButton extends LinearLayout{

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_button, this, true);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0);
        String titleStr = a.getString(R.styleable.CustomButton_title);
        String subTitleStr = a.getString(R.styleable.CustomButton_subTitle);
        int iconResId = a.getResourceId(R.styleable.CustomButton_icon, R.drawable.ic_launcher);
        a.recycle();

        TextView title = (TextView)findViewById(R.id.title);
        title.setText(titleStr);

        TextView subTitle = (TextView)findViewById(R.id.subTitle);
        subTitle.setText(subTitleStr);

        ImageView icon = (ImageView)findViewById(R.id.icon);
        icon.setImageResource(iconResId);
    }
}

自定义视图所充气的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:background="@drawable/white_box"
             android:layout_width="match_parent"
             android:layout_height="match_parent">


    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/main_button_selector"
            android:clickable="true"
            android:gravity="center"
            android:orientation="horizontal">

        <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="icon"
                android:layout_marginLeft="5dip"
                />

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:orientation="vertical">

            <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/grey_text"
                    android:textSize="@dimen/mainTextSize"
                    android:textStyle="bold"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/subTitle"
                    android:ellipsize="end"
                    android:maxLines="2"
                    android:textColor="@color/grey"
                    android:textSize="@dimen/mainSubTextSize"/>
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

这是我在 XML 布局中使用它的方法:

 <com.customviews.CustomButton
                android:id="@+id/pay"
                android:layout_weight="1"
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                custom:title="Hello World"
                custom:subTitle="Subtitle"
                custom:icon="@drawable/ic_launcher"/>

以及如何设置点击侦听器的活动:

 CustomButton button = (CustomButton) findViewById(R.id.pay);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity.this, "Hello World!", Toast.LENGTH_LONG).show();

我已经看到了几个关于这个问题的帖子,但没有一个能帮到我。非常感谢任何帮助。


你的CustomButton继承自LinearLayout。而你正在尝试为其设置一个onClickListener。这是你想要的吗? - SKK
是的,类似那样。但同时CustomButton需要有一个背景选择器。 - Andy Res
我在任何地方都找不到button.setClickable(true),这正常吗? - wazaminator
它在 XML 布局中设置为第二个 ViewGroup,如下所示:android:clickable="true" - Andy Res
我有一个类似的问题,我的自定义视图有FramLayout作为父ViewGroup,并将clickable属性设置为true,但是clickListener不会被调用。当我按照你说的方法删除它时,它可以工作,但是我需要这个属性来显示涟漪效果,我该如何实现这一点?你的答案对我的情况没有帮助。 - Burhan Khanzada
现在这很容易:https://dev59.com/KW025IYBdhLWcg3w9quQ#66733216 - Fattie
1个回答

22

我担心如果不设置android:clickable="true"将无法触发选择器背景,但在查看了来自View类的setOnClickListener()方法后,我发现setClickable(true)被调用了:

public void setOnClickListener(OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
}

因此,从布局声明中删除 android:clickable="true",并仅为我的自定义按钮设置点击监听器解决了这个问题。


很好的发现。 但并不清楚为什么会这样。 View.setOnClickListener() 将 clickable 设置为 true,并在任何情况下设置监听器。 - Taras Lozovyi
2
遇到了同样的问题,去掉标志就可以解决。如果设置了这些标志,在您的XML文件中,LinearLayout(子项)会获得单击回调,而您正在侦听父项CustomButton上的单击。因此,通过设置该标志,您的LinearLayout会吃掉您未处理且未传播到父项的单击。删除它将使LinearLayout不可点击,因此单击将传递到父项CustomButton,这就是为什么它起作用的原因。 - gsb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接