未为自定义视图触发OnClickListener [英] OnClickListener not triggered for Custom View

查看:86
本文介绍了未为自定义视图触发OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义按钮,该按钮是从XML布局扩展而来的.一切正常,除了没有触发点击侦听器.
我怀疑问题是由于 android:clickable ="true" 属性引起的,因为当我删除它时,会触发点击侦听器.但是我需要设置此属性,因为我的自定义视图使用选择器作为背景,如果我将其删除,则选择器将不再起作用.

I have created a custom button which is inflated from an XML layout. Everything works fine, except that the click listener is not triggered.
I suspect the problem is because of android:clickable="true" attribute, as when I remove it the click listener is triggered. But I need to have this attribute set as my custom view uses a selector as background, if I remove it, then the selector won't work anymore.

这是类的定义:

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布局:

The XML layout that the custom view is inflated from:

<?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布局中使用它的方式:

And here's how I use it in the XML layouts:

 <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"/>

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

and how the activity sets the click listener:

 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();

我已经看到几个线程已经解决了这个问题,但是没有一个线程帮助了我.不胜感激.

I have seen several threads already addressing this issue but non of them helped me. Would appreciate any help.

推荐答案

我担心未设置 android:clickable ="true" 不会触发选择器背景,但是在查看之后在 View 类的 setOnClickListener()方法中,我看到 setClickable(true)被称为:

I was worried that not setting android:clickable="true" won't trigger the selector background, but after taking a look at the setOnClickListener() method from the View class, I saw that setClickable(true) is called:

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

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

So, removing android:clickable="true" from the layout declaration and setting just the click listener for my custom button resolved the issue.

这篇关于未为自定义视图触发OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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