gpt4 book ai didi

android - 我如何将代码分配给抽屉按钮?

转载 作者:行者123 更新时间:2023-11-29 16:46:17 24 4
gpt4 key购买 nike

我想要单击导航菜单中的按钮以启动新 Activity 。使用当前代码,单击按钮时没有任何反应。我是 Android 新手,所以如果我不理解您的代码并提出一些问题,请不要生气。

The button i want the code for is in the blue ccircle

这是我的navigation_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_account"
android:title="Home"
android:icon="@mipmap/ic_home_black_24dp">
</item>
<item android:id="@+id/nav_delete"
android:title="Delete Boards"
android:icon="@mipmap/ic_close_black_24dp">
</item>
<item android:id="@+id/nav_settings"
android:title="Settings"
android:icon="@mipmap/ic_settings_black_24dp">
</item>

这是我的抽屉ma​​inActivity.java代码:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();

@Override
public boolean onOptionsItemSelected(MenuItem item) {

if (mToggle.onOptionsItemSelected(item)){

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.nav_delete) {
//call new activity
}
}

return super.onOptionsItemSelected(item);
}

}

MainActivity.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
tools:context="com.studios.cookie.chanomatic.MainActivity"
android:id="@+id/drawerLayout">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.02"
android:text="Boards"
android:textAlignment="center"
android:textColor="@android:color/black" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id = "@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

最佳答案

在您的onCreate 方法中添加以下代码:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

在您的 mainactivity 类中实现 lister:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener

XML 文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
android:fitsSystemWindows="true"
tools:openDrawer="start">

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

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

然后在你的 onNavigationItemSelected 方法中看起来像这样:

   @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_delete) {
//call new activity
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

希望对你有帮助

关于android - 我如何将代码分配给抽屉按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com