【发布时间】: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