android - 自定义 View 未触发 OnClickListener

标签 android

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

这是类定义:

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

扩展自定义 View 的 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"/>

以及 Activity 如何设置点击监听器:

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

我已经看到几个线程已经解决了这个问题,但没有一个对我有帮助。非常感谢任何帮助。

最佳答案

我担心不设置 android:clickable="true" 不会触发选择器背景,但在查看了 setOnClickListener() 方法后View 类,我看到调用了 setClickable(true):

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

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

关于android - 自定义 View 未触发 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792238/

相关文章:

c# - Android客户端自动寻找C#服务器

java - 使用 Android 的 Firebase 电子邮件和密码身份验证 - 用户注册

android - 调用 requiresPermission 注释方法时缺少权限提示

android - 我有一个用 XML 制作的自定义编辑文本,仅在 KITKAT 版本中它具有黑色背景

android - ListView IndexOutOfBoundsException异常

javascript - Ajax/ header 不匹配?

java - 在我的主线程中的 AsyncTask(Android) 中使用 "OnProgressUpdate"之外的信息

android - 在运行时向应用程序菜单添加多个启动器快捷方式

Java MessageFormat - 问题转义字符

android - 替换 "vpn.connectivity"广播事件