У меня такой код:
MainActivity.java:
package asus.example.com.fitnessapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_items,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout 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 = ".MainActivity">
<android.support.design.widget.BottomNavigationView
android:layout_width = "match_parent"
android:layout_height = "52dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:menu = "@menu/nav_items"/>
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/textView"/>
</android.support.constraint.ConstraintLayout>
nav_items.xml:
<?xml version = "1.0" encoding = "utf-8"?>
<menu xmlns:android = "http://schemas.android.com/apk/res/android">
<item
android:id = "@+id/home"
android:title = "@string/home"
android:icon = "@drawable/home" />
<item
android:id = "@+id/notification"
android:title = "@string/notifications"
android:icon = "@drawable/notification" />
<item
android:id = "@+id/profile"
android:icon = "@drawable/person"
android:title = "@string/profile" />
</menu>
Когда пользователь нажимает на любой элемент нижней панели навигации, программа должна добавить в текстовое поле имя этого элемента меню. Вот почему в коде есть следующие строки, например textView.setText("Home"); и другие. Но когда пользователь нажимает на любой элемент, ничего не отображается int textview. Что случилось?




Вы используете BottomNavigationView, а не меню. Вам нужно реализовать BottomNavigationView.OnNavigationItemSelectedListener следующим образом:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
а затем вам нужно установить слушателя в коде:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
BottomNavigationView bottomNav = findViewById(R.id.bottomNav);
bottomNav.setNavigationItemSelectedListener(this);
}
Чтобы это работало, вам нужно добавить android:id = "@+id/bottomNav" в ваш файл макета:
<android.support.design.widget.BottomNavigationView
android:id = "@+id/bottomNav"
android:layout_width = "match_parent"
android:layout_height = "52dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:menu = "@menu/nav_items"/>
И, наконец, метод обработки события OnNavigationItemSelectedListener:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Вам не понадобится onOptionsItemSelected() или onCreateOptionsMenu(), если вы просто хотите использовать BottomNavigationView.