簡體   English   中英

如何使我的抽屜布局中包含的按鈕可點擊?

[英]How to make clickable the included button inside my drawer layout?

我想單擊抽屜布局中包含的按鈕。 我該怎么做?

這是 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.views.MainActivity"
tools:openDrawer="start">

<include
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@layout/activity_home"></include>

<androidx.appcompat.widget.Toolbar
    android:id="@+id/ts_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"></androidx.appcompat.widget.Toolbar>

<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"
    app:headerLayout="@layout/drawer_header"
    app:itemIconSize="35dp"
    app:itemTextAppearance="@style/TextAppearance.AppCompat.Medium.Inverse"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/nav_menu"></com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

val navView = findViewbyId(R.id.nav_View)
navView.setNavigationItemSelectedListener { item ->
    when(item.itemId) {
        R.id.YOUR_ID -> {
            //click
        }
    }

    true
}

好吧,這是 java 中的一個示例:

Button btn = findViewById(R.id.btn_create_order);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.btn_create_order: 
                //Do something...
                Toast.makeText(MainActivity.this, "Hello World!", Toast.LENGTH_SHORT).show();
            break;
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM