在TextInputLayout内单击EditText会生成两个或多个事件

时间:2018-02-16 16:41:42

标签: android android-edittext ontouchlistener android-textinputlayout ontouch

我在EditText中有一个TextInputLayout

    <android.support.design.widget.TextInputLayout
    android:id="@+id/inputLayoutDoB"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/stepViewIndicator"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
    app:errorTextAppearance="@style/Text.Appearance.App.ErrorText"
    android:textColorHint="@color/colorAccentLight">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/inputDoB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/ic_profile_calendar"
        android:drawablePadding="@dimen/form_drawable_margin"
        android:hint="@string/strHintDoB"
        android:textSize="@dimen/input_text"
        android:inputType="date"
        android:textColor="@android:color/white"/>

</android.support.design.widget.TextInputLayout>

我的RelativeLayout旨在防止禁用视图自动对焦:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorAccent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:focusableInTouchMode="true">

现在,我需要在上面的EditText上管理触摸手势,弹出一个简单的AlertDialog

inputDoB = findViewById(R.id.inputDoB);
// *** PREVENT SOFT KEYBOARD APPEARANCE
inputDoB.setInputType(InputType.TYPE_NULL);
inputDoB.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            AlertDialog alertDialog = new 
            AlertDialog.Builder(ProfileActivity_Step1.this).create();
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Alert message to be shown");
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            alertDialog.show();
            return false;
        }
    });

似乎一切正常,但当我点击/触摸EditText时,会出现 TWO或MORE AlertDialog。

此行为是否与EditText容器(TextInputLayout)相关? 如果是,我该如何解决这个问题?

有什么想法吗?

非常感谢您的支持

1 个答案:

答案 0 :(得分:0)

onTouch()方法可以被多次调用,因为你(作为一个人)认为它是一个&#34;单个&#34;触摸。至少会有一个ACTION_DOWN事件,然后是ACTION_UPACTION_CANCEL事件。但是,如果你在屏幕上移动手指,你也会得到ACTION_MOVE个事件。

所以,将你的方法改为:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        AlertDialog alertDialog = new 
        AlertDialog.Builder(ProfileActivity_Step1.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
    return false;
}

此外,如果您希望真正拦截触摸事件(并防止提示向上滑动,就像EditText具有焦点一样),您应该将其更改为return true