我正在创建一个指南应用,我希望其他用户点击导航抽屉上的某个项目,如何让它打开另一个活动? 这是MainApplication.java:
package net.agiann.paliachora_guide;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
答案 0 :(得分:1)
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.drawer_home:
Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
startActivity(intent);
finish();
return true;
// For rest of the options we just show a toast on click
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
return true;
}
}
});
答案 1 :(得分:0)
试试这个:
实施您的活动 NavigationView.OnNavigationItemSelectedListener
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent intent=new Intent(this,HomeActivity.class);
startActivity(intent);
}else if(second codition){
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 2 :(得分:0)
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.nav_item_Animation:
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.nav_item_loginScreen:
Intent intent=new Intent(this,LoginActivity.class);
startActivity(intent);
break;
}
return false;
});