单击导航抽屉上的项目打开虚假活动

时间:2017-07-24 20:38:33

标签: java android xml navigation navigation-drawer

我编写了一个测验app.There我还有一个带有项目的导航抽屉:Home,stats和about。第一个Activity / main活动(quizActivity)具有home项,第二个Activity(menu2)具有stats项。 Menu3有关于项目。现在我已经通过点击主页 - >项目在导航抽屉的Menu2中设置了它QuizActivity打开。现在我也想要,当我点击about按钮时,Menu3将会打开。但是,而不是Menu3,我在打开之前使用的最近的应用程序/它打开应用程序的最近页面,如测验活动。我的错是什么?我检查了其他类似的线程,但找不到帮助。

Menu2 Java:

       public class Menu2 extends AppCompatActivity {




private DrawerLayout mDrawerLayout2;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private Button popup;
private Button popup2;
private Button popup3;
private PopupWindow popupWindow;private LayoutInflater layoutInflater; //Alows to add a new layout in our window



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu2);



    TextView txtScore = (TextView) findViewById(R.id.textScore2);
    TextView txtHighScore = (TextView) findViewById(R.id.textHighScore);
    ImageView imgTrophyView1 = (ImageView) findViewById(R.id.trophy1);
    ImageView imgTrophyView2 = (ImageView) findViewById(R.id.trophy2);
    Button bttPOPUP = (Button) findViewById(R.id.enablePOPUP);
    Button bttPOPUP2 =(Button)findViewById(R.id.enablePOPUP2);
    Button bttPOPUP3 = (Button) findViewById(R.id.enablePOPUP3);
    ImageView imgTrophyView3=(ImageView)findViewById(R.id.trophy3);


    Intent intent = getIntent();
    int mScore = intent.getIntExtra("score", 0);
    txtScore.setText("Your score is: " + mScore);



    SharedPreferences mypref = getPreferences(MODE_PRIVATE);
    int highScore = mypref.getInt("highScore", 0);
    if (highScore >= mScore){
        txtHighScore.setText("High score: " + highScore);




        if (highScore >= 10) {
            imgTrophyView1.setVisibility(View.VISIBLE);
            bttPOPUP.setVisibility(View.VISIBLE);

        }
        if (highScore >= 20) {
            imgTrophyView2.setVisibility(View.VISIBLE);
            bttPOPUP2.setVisibility(View.VISIBLE);

        }

        if (highScore >= 30) {
            imgTrophyView3.setVisibility(View.VISIBLE);
            bttPOPUP3.setVisibility(View.VISIBLE);

        }



    }



    else {
        txtHighScore.setText("New highscore: " + mScore);

        SharedPreferences.Editor editor = mypref.edit();
        editor.putInt("highScore", mScore);
        editor.commit();
    }



    //Start POPUP window 1

    popup = (Button)findViewById(R.id.enablePOPUP);
    popup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layoutInflater =(LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.popup_menu2_1,null);
            popupWindow = new PopupWindow(container,1000,980,true); //400,400=popUp size, true = makes that we can close the pop up by simply click out of the window
            popupWindow.showAtLocation(mDrawerLayout2, Gravity.CENTER, 0, 0);
            mDrawerLayout2.setAlpha((float) 0.3);

            container.setOnTouchListener(new View.OnTouchListener(){

                @Override

                public boolean onTouch(View view, MotionEvent motionEvent  ){
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                    return true;

                }

            });


            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                }
            });
        }
    });

    //End POPUP window 1
    //start POPUP window 2

    popup2 = (Button)findViewById(R.id.enablePOPUP2);
    popup2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layoutInflater =(LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.popup_menu2_2,null);
            popupWindow = new PopupWindow(container,1000,980,true); //400,400=popUp size, true = makes that we can close the pop up by simply click out of the window
            popupWindow.showAtLocation(mDrawerLayout2, Gravity.CENTER, 0, 0);
            mDrawerLayout2.setAlpha((float) 0.3);

            container.setOnTouchListener(new View.OnTouchListener(){

                @Override

                public boolean onTouch(View view, MotionEvent motionEvent  ){
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                    return true;

                }

            });


            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                }
            });
        }
    });

//End POPUP window 2
    //start POPUP window 3

    popup3 = (Button)findViewById(R.id.enablePOPUP3);
    popup3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layoutInflater =(LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.popup_menu2_3,null);
            popupWindow = new PopupWindow(container,1000,980,true); //400,400=popUp size, true = makes that we can close the pop up by simply click out of the window
            popupWindow.showAtLocation(mDrawerLayout2, Gravity.CENTER, 0, 0);
            mDrawerLayout2.setAlpha((float) 0.3);

            container.setOnTouchListener(new View.OnTouchListener(){

                @Override

                public boolean onTouch(View view, MotionEvent motionEvent  ){
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                    return true;

                }

            });


            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    mDrawerLayout2.setAlpha((float) 1);
                    popupWindow.dismiss();

                }
            });
        }
    });

    //End POPUP window 3



    mToolbar = (Toolbar)findViewById(R.id.nav_action);
    setSupportActionBar(mToolbar);
    mDrawerLayout2 = (DrawerLayout) findViewById(R.id.drawerLayout2);

    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout2, R.string.open, R.string.close);
    mDrawerLayout2.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView mNavigationView = (NavigationView) findViewById(nv2);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            Intent i;


            if (id == R.id.nav_home2) {
                // Handle the home action
                i = new Intent(Menu2.this, QuizActivity.class);
                startActivity(i);
            } else if (id == R.id.nav_about2) {
                i = new Intent(Menu2.this, Menu3.class);
                startActivity(i);
            }

            return true;
        }

        });
    }









@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);




}

}

导航菜单:

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

     <item android:id="@+id/nav_home2"
    android:title="Home"
    android:icon="@mipmap/ic_home_black_24dp"/>

<item android:id="@+id/nav_stats2"
    android:title="Stats"
    android:icon="@mipmap/ic_person_black_24dp"
    android:checked="true"/>

<item android:id="@+id/nav_about2"
    android:title="About"
    android:icon="@mipmap/ic_info_outline_black_24dp"/>

</menu>

菜单2 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="amapps.impossiblequiz.Menu2"
          android:id="@+id/drawerLayout2"
          android:orientation="vertical">


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


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


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">




        <TextView
            android:id="@+id/textScore2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#f60"
            android:paddingTop="10dp"
            android:paddingLeft="35dp"
            android:text="Your score is:"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_marginTop="28dp"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:id="@+id/textHighScore"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#FFAB00"
            android:paddingLeft="35dp"
            android:paddingRight="100dp"
            android:paddingTop="10dp"
            android:text="Highest score:"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_marginTop="13dp"
            android:layout_below="@+id/textScore2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:id="@+id/tryAgain_button"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:background="@drawable/bg_pill"
            android:onClick="onClick"
            android:text="Restart Quiz!"
            android:textColor="#ffffff"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/textHighScore"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="33dp"
            android:text="Stats: play and get trophies!"
            android:textAppearance="@style/TextAppearance.AppCompat.Button"
            android:textSize="20dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />





        <ImageView
            android:id="@+id/statsTrophy"

            app:srcCompat="@drawable/statsoftrophy"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignTop="@+id/textHighScore"
            android:layout_above="@+id/tryAgain_button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <ImageView
            android:id="@+id/trophy1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tryAgain_button"
            android:layout_marginLeft="26dp"
            android:layout_marginStart="26dp"
            android:layout_marginTop="70dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/trophy1" />

        <ImageView
            android:id="@+id/trophy2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignTop="@+id/trophy1"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:layout_toEndOf="@+id/trophy1"
            android:layout_toRightOf="@+id/trophy1"
            android:visibility="invisible"
            app:srcCompat="@drawable/trophy2" />

        <Button
            android:id="@+id/enablePOPUP"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:text="Info"
            android:alpha="0.30"
            android:visibility="invisible"
            android:background="@drawable/bg_pill_trophy"
            android:textColor="#ffffff"
            android:layout_alignTop="@+id/enablePOPUP2"
            android:layout_alignLeft="@+id/trophy1"
            android:layout_alignStart="@+id/trophy1" />

        <Button
            android:id="@+id/enablePOPUP2"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:background="@drawable/bg_pill_trophy"
            android:text="Info"
            android:textColor="#ffffff"
            android:alpha="0.30"
            android:visibility="invisible"
            android:layout_marginTop="12dp"
            android:layout_below="@+id/trophy2"
            android:layout_alignLeft="@+id/trophy2"
            android:layout_alignStart="@+id/trophy2"
            android:layout_alignRight="@+id/trophy2"
            android:layout_alignEnd="@+id/trophy2" />

        <ImageView
            android:id="@+id/trophy3"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignTop="@+id/trophy2"
            android:layout_toLeftOf="@+id/statsTrophy"
            android:layout_toStartOf="@+id/statsTrophy"
            app:srcCompat="@drawable/trophy3"
            android:visibility="invisible"/>

        <Button
            android:id="@+id/enablePOPUP3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:background="@drawable/bg_pill_trophy"
            android:text="Info"
            android:textColor="#ffffff"
            android:alpha="0.30"
            android:visibility="invisible"
            android:layout_alignBottom="@+id/enablePOPUP2"
            android:layout_alignLeft="@+id/trophy3"
            android:layout_alignStart="@+id/trophy3"
            android:layout_alignTop="@+id/enablePOPUP2"
            android:layout_alignRight="@+id/trophy3"
            android:layout_alignEnd="@+id/trophy3" />


    </RelativeLayout>




</LinearLayout>


<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu2"
    android:layout_gravity="start"
    android:id="@+id/nv2"
    app:headerLayout="@layout/navigation_header"
    app:itemIconTint="@drawable/tint_color_selector2">




</android.support.design.widget.NavigationView>

Menu3 Java:

        public class Menu3 extends AppCompatActivity {

private DrawerLayout mDrawerLayout3;
private ActionBarDrawerToggle mToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu3);


    Toolbar mToolbar = (Toolbar) findViewById(R.id.nav_action);
    setSupportActionBar(mToolbar);
    mDrawerLayout3 = (DrawerLayout) findViewById(R.id.drawerLayout);

    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout3, R.string.open, 
    R.string.close);
    mDrawerLayout3.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView mNavigationView = (NavigationView) findViewById(nv3);
    mNavigationView.setNavigationItemSelectedListener(new 
    NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem){
            switch (menuItem.getItemId()){
                case(R.id.nav_home3):
                    Intent accountActivity3 = new 
         Intent(getApplicationContext(),QuizActivity.class);
                    startActivity(accountActivity3);

            }
            return true;
        }
    });}

public void onClick(View view) {

    Intent intent = new Intent(Menu3.this, QuizActivity.class);
    startActivity(intent);

}




@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}

 }

Menu3 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="amapps.impossiblequiz.Menu3"
      android:id="@+id/drawerLayout3">


<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu3"
    android:layout_gravity="start"
    android:id="@+id/nv3"
    app:headerLayout="@layout/navigation_header"
    app:itemIconTint="@drawable/tint_color_selector2">




</android.support.design.widget.NavigationView>

2 个答案:

答案 0 :(得分:1)

尝试将 onNavigationItemSelected()更改为:

    public boolean onNavigationItemSelected(MenuItem menuItem) {

             switch(menuItem.getItemId()){
                 case R.id.nav_home2:
                 startActivity(new Intent(Menu2.this, QuizActivity.class));
                 break;
                 case R.id.nav_about2:
                 startActivity(new Intent(Menu2.this, Menu3.class));
                 break;
             }

                return true;
     }

答案 1 :(得分:0)

问题是你有3个活动,这取决于你去哪个活动。因此,您必须在多个位置使用与您单击的项目相同的代码。

更好的方法是使用片段。

您应该查看本教程: https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

同时检查MVP设计模式。