Android Studio中的Navigation Drawder中的OnClick项目
我知道这很常见,但是我真的很困惑如何将OnClickListener应用于我的Navigation Drawder项目,我有两个项目是编辑配置文件和注销,我只想toast
或打印其中一个项目被选中了,有人可以帮助我解决这个问题,我已经在互联网上搜索过,但是我发现的onclicklistener,需要帮助,
我认为我的mainActivity
是<代码> oncreate()
increate()mainActivity
setContentView(R.layout.dashboard_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);
sidemenu.xml.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>
dashboard_main_main_main_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 >
更新我尝试了下面的代码
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;
}
});
I know this is common to ask but I really confused about how can I apply onClickListener to my navigationDrawer items, I have two items which is edit profile and Logout I just want to Toast
or print if one of them is selected, Is there anyone can help me with this, I've already search on the internet , yet there's no OnClickListener I've found, need help
I think there's missing on my MainActivity
which is the onCreate()
OnCreate() MainActivity
setContentView(R.layout.dashboard_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);
sidemenu.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>
dashboard_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 >
Updated I tried the code below but when I click the items it will automatically hide back the sidebar and there are no Toast
pops
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;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要在活动上实现NavigationView.OnNavigationItemSelectedListener接口。
然后在您的OnCreate()方法类型上
navigationView.setNavigationItemSelectedListener(this);在此之后,覆盖了方法。
在这里,您可以使用此示例代码。
First you need to implement NavigationView.OnNavigationItemSelectedListener interface on your Activity.
then on your onCreate() method type
navigationView.setNavigationItemSelectedListener(this); after this override the method onNavigationItemSelected.
here you can use this sample code.