![](/img/trans.png)
[英]Android Studio | Button onClick in a NavigationDrawer Fragment
[英]OnClick item in NavigationDrawer in Android Studio
我知道这很常见,但我真的很困惑如何将 onClickListener 应用于我的 navigationDrawer 项目,我有两个项目,即编辑配置文件和注销我只想Toast
或打印,如果其中一个被选中,有没有人可以帮帮我,我已经在互联网上搜索了,但我没有找到 OnClickListener,需要帮助
我认为我的MainActivity
缺少onCreate()
OnCreate() 主活动
setContentView(R.layout.dashboard_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);
边菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior = "single">
<item android:id="@+id/editProfile"
android:icon="@drawable/history"
android:title="Edit Profile"/>
<item android:id="@+id/logout_menu"
android:icon="@drawable/logout"
android:title="Logout"/>
</group>
sidebar_header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@color/dashboard_item_1"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp"
>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/user_logo"
app:civ_border_width="2dp"
app:civ_border_color="#FFFFFF"/>
<TextView
android:id="@+id/fullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/idNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Id no."
android:textColor="#FFFFFF"
android:textSize="12sp"/>
</LinearLayout>
仪表板_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:id="@+id/drawerlayout"
tools:context=".MainActivity">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigationview"
app:headerLayout="@layout/sidebar_header"
app:menu="@menu/sidemenu"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_gravity="start"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Toolbars"
app:titleTextColor="#FFFFFF"
app:titleMarginStart = "10dp"
android:layout_marginBottom="5dp"
tools:ignore="MissingConstraints"/>
<View
android:id="@+id/viewHeaderBackground"
android:layout_width="match_parent"
android:layout_height="@dimen/_125sdp"
android:background="@color/primary"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:layout_marginLeft="@dimen/_16sdp"
android:layout_marginTop="@dimen/_16sdp"
android:text="Dashboard"
android:textColor="@color/white"
android:textSize="@dimen/_20ssp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/_26sdp"
android:layout_height="@dimen/_26sdp"
android:layout_marginEnd="@dimen/_16sdp"
android:layout_marginRight="@dimen/_16sdp"
android:src="@drawable/user_logo"
app:layout_constraintBottom_toBottomOf="@id/textTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/textTitle"
/>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:layout_marginEnd="@dimen/_16sdp"
app:cardBackgroundColor="@color/card_background"
app:cardCornerRadius="@dimen/_16sdp"
app:layout_constraintBottom_toBottomOf="@id/viewHeaderBackground"
app:layout_constraintTop_toBottomOf="@id/viewHeaderBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/_14sdp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overview"
android:textColor="@color/primary_text"
android:textSize="@dimen/_14ssp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageButton
android:id="@+id/textViews"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@android:color/transparent"
android:layout_marginTop="@dimen/_8sdp"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="@+id/layoutClients"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_8sdp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@id/layoutImpacted"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViews">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/_28sdp"
android:layout_height="@dimen/_28sdp"
android:src="@drawable/ic_clients"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="Pending"
android:textColor="@color/secondary_text"
android:textSize="@dimen/_10ssp"/>
<TextView
android:id="@+id/txtPending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="0"
android:textColor="@color/primary_text"
android:textSize="@dimen/_16ssp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layoutImpacted"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_8sdp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@id/layoutFollowing"
app:layout_constraintStart_toEndOf="@id/layoutClients"
app:layout_constraintTop_toBottomOf="@id/textViews">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/_28sdp"
android:layout_height="@dimen/_28sdp"
android:src="@drawable/ic_impacted"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="Approved"
android:textColor="@color/secondary_text"
android:textSize="@dimen/_10ssp"/>
<TextView
android:id="@+id/txtApproved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="0"
android:textColor="@color/primary_text"
android:textSize="@dimen/_16ssp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layoutFollowing"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_8sdp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/layoutImpacted"
app:layout_constraintTop_toBottomOf="@id/textViews">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="@dimen/_28sdp"
android:layout_height="@dimen/_28sdp"
android:src="@drawable/ic_following"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="Records"
android:textColor="@color/secondary_text"
android:textSize="@dimen/_10ssp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_4sdp"
android:text="0"
android:textColor="@color/primary_text"
android:textSize="@dimen/_16ssp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
android:overScrollMode="never"
android:padding="@dimen/_16sdp"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/cardHeader">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/dashboard_item_1"/>
<include layout="@layout/dashboard_item_2"/>
<include layout="@layout/dashboard_item_3"/>
<include layout="@layout/dashboard_item_4"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout >
更新后我尝试了下面的代码,但是当我单击项目时,它会自动隐藏侧边栏并且没有Toast
弹出
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
if (item.getItemId() == R.id.editProfile)
{
Toast.makeText(getApplicationContext(),"profile",Toast.LENGTH_SHORT).show();
}
else if (item.getItemId() == R.id.logout_menu)
{
Toast.makeText(getApplicationContext(),"logout",Toast.LENGTH_SHORT).show();
}
return false;
}
});
首先,您需要在 Activity 上实现 NavigationView.OnNavigationItemSelectedListener 接口。
然后在你的 onCreate() 方法上输入 navigationView.setNavigationItemSelectedListener(this); 在此之后覆盖方法 onNavigationItemSelected。
在这里你可以使用这个示例代码。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.editProfile)
{
//edit your profile
}
else if (item.getItemId() == R.id.logout)
{
//log out
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.