如何解决为什么 TouchListener 不能处理自定义警报对话框?
我实现了一个扩展 Dialog 并实现 OnTouchListener 的类,布局有两个 TextViews,第三个用作按钮,我使用 TouchListener 更改 ActionDown 上的按钮颜色并使用 ActionUp 上的 performClick() 但对话框是没有检测到任何触摸事件。它曾经可以工作,但后来我尝试添加第二个按钮,以及一些设置每个按钮的可见性和 onClick() 的方法,但由于这不起作用,因为我收到错误消息,说我正在调用 OnClickListener null 引用我试图恢复。但现在它不检测触摸事件。
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 中的状态,背景可绘制有很多可能性。您正在寻找的典型事件是 OnClickListener
。自己在 onTouch 中管理视觉外观往往会产生问题,我建议仅针对特定需求这样做。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。