Android:当 EditText 是子级时如何使布局可点击

标签 android android-layout android-view

我有一个相对布局,其中有一个 EditText 和一个 ImageView 。 在某些情况下,我想让整个布局可单击,而不是其任何子布局。

我在布局上添加了一个 OnClickListener。我和 children 一起尝试了以下方法: 1. setEnabled(假) 2. setClickable(false)

这适用于 ImageView,但即使在上述更改之后,当我单击 EditText 附近的区域时,键盘会出现,我可以在编辑文本中看到光标。 相反,我希望所有点击/触摸事件都进入布局。

有人可以帮忙吗? 谢谢

最佳答案

您将创建一个 CustomLayout 类并重写 onInterceptTouchEvent 方法。如果该方法返回true,则布局的子级将不会接收触摸事件。您可以创建一个成员变量和一个公共(public) setter 来更改返回值。

自定义布局类

public class CustomLayout extends LinearLayout {

    //If set to false, the children are clickable. If set to true, they are not.
    private boolean mDisableChildrenTouchEvents;

    public CustomLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mDisableChildrenTouchEvents = false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mDisableChildrenTouchEvents;
    }

    public void setDisableChildrenTouchEvents(boolean flag) {
        mDisableChildrenTouchEvents = flag;
    }
}

主要 Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomLayout layout = findViewById(R.id.mylayout);

        //Disable touch events in Children
        layout.setDisableChildrenTouchEvents(true);

        layout.setOnClickListener(v -> System.out.println("Layout clicked"));
    }
}

XML 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<com.example.dglozano.myapplication.CustomLayout
    android:id="@+id/mylayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/outline"
    android:clipChildren="true"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter email"
        android:inputType="textEmailAddress"
        android:layout_gravity="center"/>
</com.example.dglozano.myapplication.CustomLayout>


</android.support.constraint.ConstraintLayout>

关于Android:当 EditText 是子级时如何使布局可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384251/

相关文章:

android - 适配器中的膨胀布局忽略大小

java - EditText 不显示文本

android - 有什么方法可以在 ImageView 上进行动画裁剪?

android - 无法解析符号 'quickstart'

android - 防止工具栏在 recyclerview 有一项时隐藏

java - 如何在 Android Studio 中从 SimonVT 导入 MenuDrawer

Android - 如何保存以编程方式创建的 View 并在应用重启时恢复它

java - IndexOutOfBoundsException 导致 Android 应用程序崩溃

android - 将图标与工具栏图标对齐 - Android Material Design

Android:抽象自定义 View 和常见布局膨胀