我的应用程序中有一个导航抽屉和一个包含的布局。但是当我运行应用程序时,带有 onClickListener 的每个图像和按钮都不起作用。在下面的代码中,不包含 navigationDrawer 代码,因为我希望单击其他按钮。我不知道是否有隐形布局挡住了一切还是什么
带有包含标签的主活动布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
tools:openDrawer="start"
android:layout_height="match_parent">
<include
layout="@layout/content_dg_main_2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorPrimary"
app:headerLayout="@layout/navigation_drawer_menu_header"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
这是另一个带有按钮和图像的布局(content_dg_main_2)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_speedo_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/ivNavMenu"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:padding="8dp"
android:scaleType="fitXY"
android:src="@drawable/menu"
android:tint="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivUpgrade"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="10dp"
android:padding="4dp"
android:src="@drawable/ic_upgrade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/colorBNBSelected" />
<TextView
android:id="@+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Video Recorder"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/ivUpgrade"
app:layout_constraintStart_toEndOf="@+id/ivNavMenu"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="consume"
android:text="Button"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/clHeader">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnstart"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:onClick="onclick"
android:background="@mipmap/ic_launcher"
app:layout_constraintBottom_toTopOf="@+id/btnstop"
app:layout_constraintDimensionRatio="482:317"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.6" />
<TextView
android:id="@+id/tvTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="00:00:00"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/btnstart" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/blue_border_bg"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTimer"
app:layout_constraintWidth_percent="0.6">
<ImageView
android:id="@+id/gallryicon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:textColor="@color/black"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/gallryicon"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_setting"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/green_border_bg"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_gallery"
app:layout_constraintWidth_percent="0.6">
<ImageView
android:id="@+id/settingicon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Setting"
android:textColor="@color/black"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/settingicon"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/btnstop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop service"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnstart" />
<FrameLayout
android:id="@+id/adViewFrameLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_setting" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
java代码
startButton = findViewById(R.id.btnstart);
navMenuImageView = findViewById(R.id.ivNavMenu);
upgradeImageView = findViewById(R.id.ivUpgrade);
timerTextView = findViewById(R.id.tvTimer);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainDGAActivity.this, "start button Clicked", Toast.LENGTH_SHORT).show();
}
});
navMenuImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.closeDrawer(GravityCompat.START);
}
});
upgradeImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a dialog to display the upgrade layout
final Dialog upgradeDialog = new Dialog(MainDGAActivity.this);
upgradeDialog.setContentView(R.layout.purchase_dg_dialog); // Replace with your upgrade layout XML file
// Optionally, set dialog properties like background, title, etc.
// For example:
/* upgradeDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
upgradeDialog.setTitle("Upgrade");*/
// Add any logic or UI interactions related to your upgrade screen here
// Add a button to close the dialog
Button closeButton = upgradeDialog.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
upgradeDialog.dismiss(); // Close the dialog when the close button is clicked
}
});
// Show the dialog
upgradeDialog.show();
}
});
这不是一个答案,但尝试看看 setOnClickListener 是否在按钮上工作。 将此代码写入您的 setOnClickListeners
Log.w("点击按钮监听器", "监听按钮:成功");
或者你可以尝试一下
Log.w("点击按钮监听器","监听按钮:"+button.isPressed());
然后在Logcat中查看结果。