Android:当EditText是子级时,如何使布局可单击 [英] Android: How to make Layout clickable when EditText is a child

查看:60
本文介绍了Android:当EditText是子级时,如何使布局可单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对布局,里面有一个EditText和一个ImageView.在某些情况下,我想使整个布局可点击,而不是使其任何子代变为可点击.

I have a Relative Layout with an EditText and an ImageView inside it. Under certain circumstances, I would like to make the whole layout clickable and not any of its children.

我在布局上添加了OnClickListener.我和孩子们尝试了以下方法:1. setEnabled(false)2. setClickable(false)

I added an OnClickListener on the layout. And I tried the following with the children: 1. setEnabled(false) 2. setClickable(false)

这适用于ImageView,但是即使进行了上述更改,当我单击EditText附近的区域时,键盘也会出现,并且可以在编辑文本中看到光标.取而代之的是,我希望所有单击/触摸事件都进入布局.

This works for the ImageView but even after the above changes, when I click on the area near the EditText, the keyboard comes up and I can see the cursor in the edit text. Instead of that, I am hoping that all click/touch events go to the layout.

有人可以帮忙吗?谢谢

推荐答案

只需创建一个CustomLayout类并覆盖 onInterceptTouchEvent 方法.如果该方法返回 true ,则布局的子级将不会收到touch事件.您可以创建一个成员变量和一个公共设置器来更改返回值.

Yon create a CustomLayout class and override the onInterceptTouchEvent method. If that method returns true, the layout's childrens will not receive the touch event. You can create a member variable and a public setter to change the returning value.

CustomLayout类

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

MainActivity

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是子级时,如何使布局可单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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