gpt4 book ai didi

Android Studio 菜单项点击不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:43:02 25 4
gpt4 key购买 nike

所以我的菜单项除了在单击时显示放大动画外不会做任何事情。我试图与他们一起开展一项新 Activity ,我在其中一个上贴了 toast ,看看是否有任何作用,但我什么也没得到。这是一个常见问题吗?

minSdkVersion 17targetSdkVersion 27

布局

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />

布局正在使用,android.support.constraint.ConstraintLayout

菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/navigation_home"
android:icon="@drawable/home"
android:title="@string/title_home" />

<item
android:id="@+id/navigation_apps"
android:icon="@drawable/apps"
android:title="@string/title_apps" />

<item
android:id="@+id/navigation_profile"
android:icon="@drawable/profile"
android:title="@string/title_profile" />

<item
android:id="@+id/navigation_logout"
android:icon="@drawable/logout"
android:title="@string/title_logout" />

</menu>

Java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.navigation_home) {

Intent navHome = new Intent(this, WordpressActivity.class);
this.startActivity(navHome);
return true;
}

if (id == R.id.navigation_apps) {
Toast.makeText(this, "apps is Clicked", Toast.LENGTH_LONG).show();
return true;
}

if (id == R.id.navigation_profile) {
Intent navProf = new Intent(this, AccountActivity.class);
this.startActivity(navProf);
return true;
}

if (id == R.id.navigation_logout) {
Intent navLog = new Intent(this, LogoutActivity.class);
this.startActivity(navLog);
return true;
}

return super.onOptionsItemSelected(item);
}

提前感谢您对可能出现的问题提供的任何帮助或指导。我刚开始使用菜单项点击 Intent ,我真的不明白为什么它不起作用。

最佳答案

onOptionsItemSelected 并不意味着处理 BottomNavigationView 上的操作。 BottomNavigationViewMenuItem 的操作应按如下方式完成:

BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView
.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.navigation_home) {

Intent navHome = new Intent(MainActivity.this, WordpressActivity.class);
MainActivity.this.startActivity(navHome);
return true;
}

if (id == R.id.navigation_apps) {
Toast.makeText(MainActivity.this, "apps is Clicked", Toast.LENGTH_LONG).show();
return true;
}

if (id == R.id.navigation_profile) {
Intent navProf = new Intent(MainActivity.this, AccountActivity.class);
MainActivity.this.startActivity(navProf);
return true;
}

if (id == R.id.navigation_logout) {
Intent navLog = new Intent(MainActivity.this, LogoutActivity.class);
MainActivity.this.startActivity(navLog);
return true;
}
return false;

});

关于Android Studio 菜单项点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51372114/

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