【问题标题】:Android frame layout click listener not workingAndroid框架布局点击监听器不起作用
【发布时间】:2017-10-14 12:09:17
【问题描述】:

我有一个 FrameLayout 和两个嵌套的 LinearLayouts。我想要一个onClickListener() 用于FrameLayout。经过搜索,我得到了解决方案clickable="false"

这是我的布局文件:

<FrameLayout
    android:id="@+id/flOuter"
    android:paddingLeft="@dimen/half_margin"
    android:paddingRight="@dimen/half_margin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:clickable="false"
        android:gravity="center"
        android:id="@+id/flAddIdProof"
        android:background="@drawable/add_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:clickable="false"
            android:id="@+id/ivIcon"
            android:src="@drawable/ic_add_black_48dp"
            android:background="@drawable/add_button_border"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        </LinearLayout>

    <LinearLayout
        android:clickable="false"
        android:id="@+id/llLinear"
        android:alpha="0.5"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:enabled="false"
            android:clickable="false"
            android:drawablePadding="@dimen/half_margin"
            android:drawableLeft="@drawable/ic_devices_black_24dp"
            android:hint="Identity Proof"
            android:inputType="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:enabled="false"
            android:clickable="false"
            android:drawableLeft="@drawable/ic_border_color_black_24dp"
            android:hint="Identity Number"
            android:drawablePadding="@dimen/half_margin"
            android:inputType="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>

这是我的点击监听器:

flOuter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(DeviceInMeeting.this, "detect", Toast.LENGTH_SHORT).show();
    }
});

我不知道如何调试 onClick 侦听器,clickable="false" 在我的情况下不起作用。

【问题讨论】:

标签: android android-framelayout


【解决方案1】:

尝试在FrameLayout的以下代码中实现android:clickable="true"

<FrameLayout    
   android:id="@+id/flOuter"
   android:clickable="true"
   android:focusable="true"
   android:foreground="attr/selectableItemBackground"
   android:paddingLeft="@dimen/half_margin"
   android:paddingRight="@dimen/half_margin"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

并在其他嵌套布局中添加android:clickable="false"

<LinearLayout    
   android:id="@+id/flOuter"
   android:clickable="false" />

【讨论】:

    【解决方案2】:

    你用错了。尝试使用:

    <FrameLayout
        android:id="@+id/flOuter"
        android:paddingLeft="@dimen/half_margin"
        android:paddingRight="@dimen/half_margin"
        android:layout_width="match_parent"
        android:clickable="true"
        android:layout_height="wrap_content">
    
        // Other layouts there
    
    </FrameLayout>
    

    【讨论】:

    • 但我想要可点击的框架布局,而不是线性布局
    • 您在内部线性布局的宽度和高度上都使用了 match_parent。所以它覆盖了整个 FrameLayout,因此点击不起作用。明白我的意思了吗??
    • 如果我使用 textView 而不是 edittext 那么它工作...没有得到确切的解决方案
    【解决方案3】:

    对于您不想被点击的字段,添加clickable=false 到它们。 对于您不想点击事件的字段,请为它们添加clickable=true

    这里要记住的一件事是,如果您的子元素包含一些可点击的元素,例如按钮和编辑文本,然后您想在父布局上获得点击事件,则将可点击的 false 添加到子元素并将 true 添加到父元素。

    【讨论】:

      【解决方案4】:

      您需要设置OnTouchListener 而不是onClickListener。在onTouch 方法中,您可以决定是否将触摸事件传递给孩子(通过返回false)或消费事件。

      根据你的情况,你应该做你的逻辑并返回 true。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-19
        • 2021-04-24
        • 1970-01-01
        相关资源
        最近更新 更多