【问题标题】:Why isn't TouchListener working on custom alert dialog?为什么 TouchListener 不在自定义警报对话框上工作?
【发布时间】:2021-03-31 21:28:21
【问题描述】:

我实现了一个扩展 Dialog 并实现 OnTouchListener 的类,布局有两个 TextView 和第三个用作按钮,我使用 TouchListener 更改 ActionDown 上的按钮颜色和 ActionUp 上的 performClick() 但对话框是未检测到任何触摸事件。它曾经可以工作,但后来我尝试添加第二个按钮,以及一些设置每个按钮的可见性和 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>

【问题讨论】:

    标签: android android-layout android-alertdialog touch-event


    【解决方案1】:

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

    附带说明:如果您想直观地对点击事件做出反应,则根据 XML 中的状态,使用 background-Drawables 有很多可能性。您要查找的典型事件是OnClickListener。在 onTouch 中自己管理视觉外观往往会产生问题,我建议仅针对特定需求这样做。

    【讨论】:

    • 啊对,我不小心删除了setTouchListener。我试图解决这个问题好几个小时,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多