为什么 TouchListener 不能处理自定义警报对话框?

Why isn't TouchListener working on custom alert dialog?

我实现了一个 class,它扩展了 Dialog 并实现了 OnTouchListener,布局有两个 TextView,第三个用作按钮,我使用 TouchListener 更改 ActionDown 和 performClick() 上的按钮颜色在 ActionUp 上,但对话框未检测到任何触摸事件。它曾经有效,但后来我尝试添加第二个按钮,以及一些方法来设置每个按钮的可见性和 onClick() ,但是因为那不起作用,因为我收到一条错误消息,说我正在调用 OnClickListener空引用我试图还原。但现在它不检测触摸事件。

public class AlertDialog extends Dialog implements View.OnTouchListener {

    private TextView titleView;
    private TextView messageView;
    private TextView buttonViewLeft;


    private String title;
    private String message;
    private String textButtonLeft;
    private String textButtonRight;


    public void setTextButtonRight(String textButtonRight) {
        this.textButtonRight = textButtonRight;
    }


    public void setTextButtonLeft(String textButtonLeft) {
        this.textButtonLeft = textButtonLeft;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.alertdialog);

        buttonViewLeft = findViewById(R.id.alertButton);

        buttonViewLeft.setVisibility(View.VISIBLE);


        titleView = findViewById(R.id.alertTitle);
        messageView = findViewById(R.id.alertMessage);

        titleView.setText(title);
        messageView.setText(message);

        buttonViewLeft.setText(textButtonLeft);


    }

    public AlertDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        Log.e("Touch","Touch");
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                buttonViewLeft.setBackgroundColor(ContextCompat.getColor(v.getContext(), R.color.press_grey));
                buttonViewLeft.setAlpha(0.5f);

                return true;

            case MotionEvent.ACTION_UP:
                v.performClick();
                return true;

        }

        return false;
    }
}

 private void wrongCredentialsAlert(String e){

        AlertDialog alertDialog = new AlertDialog(LoginActivity.this);
        alertDialog.setTitle(getString(R.string.alert_incorrect_password_title) + e);
        alertDialog.setMessage(getString(R.string.alert_message_incorrect_password));
        alertDialog.setTextButtonLeft("OK");
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.show();

    }

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.material.card.MaterialCardView 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="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:cardCornerRadius="10dp"
    app:cardPreventCornerOverlap="false">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/alertBackground">

        <TextView
            android:id="@+id/alertTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="TextView"
            android:textColor="?attr/colorOnPrimary"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/alertMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:ems="12"
            android:gravity="center"
            android:text="@string/alert_message_incorrect_password"
            android:textColor="?attr/colorOnPrimary"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/alertTitle" />

        <TextView
            android:id="@+id/alertButton"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:textColor="@color/white"
            app:layout_constraintTop_toBottomOf="@id/separator"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />


        <View
            android:id="@+id/separator"
            android:layout_width="0dp"
            android:layout_height="0.5dp"
            android:layout_marginTop="16dp"
            android:background="@color/separator_grey"
            app:layout_constraintBottom_toTopOf="@+id/alertButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/alertMessage" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

您永远不会将 OnTouchListener 设置为任何值。 implements 仅表示它具有已在 OnTouchListener 接口中定义的功能。如果应调用 onTouch,则需要将其设置为具有 setOnTouchListener(this) 的任何视图。看来你想用你的按钮来做到这一点。

附带说明:如果您想以视觉方式对点击事件做出反应,根据 XML 中的状态,可以使用 background-Drawables 实现多种可能性。您要查找的典型事件是 OnClickListener。在 onTouch 中自行管理视觉外观往往会产生问题,我建议仅针对特定需求使用此方法。