Android: How to make Layout clickable when EditText is a child

风格不统一 提交于 2021-02-07 09:36:27

问题


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.

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

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.

Could some one help? Thanks


回答1:


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 class

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 Layout

<?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>


来源:https://stackoverflow.com/questions/53384251/android-how-to-make-layout-clickable-when-edittext-is-a-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!