【问题标题】:Add click handler to custom Layout将点击处理程序添加到自定义布局
【发布时间】:2019-10-08 12:11:42
【问题描述】:

大家好。

我将自定义布局膨胀为对象的行。

一切正常,除了我无法将 Click/LongClick 设置为行。

这就是我所做的。

    Layout_BatchRow batchRow = new Layout_BatchRow(this);
    batchRow.New(batchObject);
    batchRow.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            . . .
        }
    });
    batchRow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            . . .
        }
    });

以下是自定义布局类:

public class Layout_BatchRow extends RelativeLayout {
    public Layout_BatchRow(Context context) {
        super(context);
        inflate(getContext(), R.layout.batch_row, this);
        . . .
    }
}

这是布局 XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    tool:context=".Layout_BatchRow">

    . . .

</RelativeLayout>

调试时未到达监听器。

有什么想法吗?

【问题讨论】:

    标签: android android-custom-view android-relativelayout


    【解决方案1】:

    我认为你不需要添加这个:

    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    

    【讨论】:

    • 好的。就是这样。我觉得自己像个白痴。我可以再问你一个小问题吗?如何在侦听器内传递活动上下文(我需要创建一个 AlertDialog.Builder)。找到了 --> Activity.this
    • 您可以像这样引用活动:yourView.setOnClickListener(this)。但是你的Activity需要实现View.OnClickListener
    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多