向导航菜单项添加操作(不可点击)

时间:2017-03-13 22:10:47

标签: android menu navigation items

我已经在我的活动上完成了一个手动导航抽屉并完成了xml菜单,并添加了NavigationView.OnNavigationItemSelectedListener作为界面,但是我的菜单项仍然无法点击。 这是以下代码

package project.senior.health;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.backendless.Backendless;
import com.backendless.exceptions.BackendlessFault;



public class LoginSuccessActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
  private Button logoutButton;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;

  public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.login_success );


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


      getSupportActionBar().setDisplayHomeAsUpEnabled(true);






   ImageButton simpleImageButton = (ImageButton)findViewById(R.id.imageButton2);
    simpleImageButton.setImageResource(R.drawable.i4);
    simpleImageButton.setBackgroundColor(Color.TRANSPARENT);

    ImageButton simpleImageButton1 = (ImageButton)findViewById(R.id.imageButton3);
    simpleImageButton1.setImageResource(R.drawable.i3);
    simpleImageButton1.setBackgroundColor(Color.TRANSPARENT);

    ImageButton simpleImageButton2 = (ImageButton)findViewById(R.id.imageButton4);
    simpleImageButton2.setImageResource(R.drawable.i2);
    simpleImageButton2.setBackgroundColor(Color.TRANSPARENT);



    initUI();

  }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();



        if (mToggle.onOptionsItemSelected(item))
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

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

    if (id == R.id.Profile) {
        // Handle the camera action
    } else if (id == R.id.Nutrition) {

    } else if (id == R.id.Workouts) {
        startActivity(new Intent(LoginSuccessActivity.this, BuildTabbedAct.class));
    } else if (id == R.id.Friends) {

    } else if (id == R.id.Search) {

    }

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


    private void initUI()
  {
//    logoutButton = (Button) findViewById( R.id.logoutButton );
//
//    logoutButton.setOnClickListener( new View.OnClickListener()
//    {
//      @Override
//      public void onClick( View view )
//      {
//        onLogoutButtonClicked();
//      }
//    } );
  }

  private void onLogoutButtonClicked()
  {
    Backendless.UserService.logout( new DefaultCallback<Void>( this )
    {
      @Override
      public void handleResponse( Void response )
      {
        super.handleResponse( response );
        startActivity( new Intent( LoginSuccessActivity.this, LoginActivity.class ) );
        finish();
      }

      @Override
      public void handleFault( BackendlessFault fault )
      {
        if( fault.getCode().equals( "3023" ) ) // Unable to logout: not logged in (session expired, etc.)
          handleResponse( null );
        else
          super.handleFault( fault );
      }
    } );




  }
  public void profile(View v){
    startActivity(new Intent(LoginSuccessActivity.this, test.class));

  }
  public void nutrition(View v){
    startActivity(new Intent(LoginSuccessActivity.this, test.class));
  }
  public void build(View v){
    startActivity(new Intent(LoginSuccessActivity.this, BuildTabbedAct.class));
  }
  public void lose(View v){
    startActivity(new Intent(LoginSuccessActivity.this, test.class));
  }
}

我只测试1个意图,该项目的ID是否正确,请提出任何想法或提示?赞赏!

1 个答案:

答案 0 :(得分:0)

我看到你实现了NavigationView.OnNavigationItemSelectedListener,但是你没有用活动注册监听器(例如:NavigationView.setNavigationItemSelectedListener(this))。请检查一下!