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

Posted

技术标签:

【中文标题】通过单击导航抽屉上的项目打开虚假活动【英文标题】:False Activity opens by clicking on an Item on the Navigation drawer 【发布时间】:2017-12-30 13:46:07 【问题描述】:

我编写了一个测验应用程序。我还有一个导航抽屉,其中包含以下项目:主页、统计信息和关于。第一个活动/主要活动 (quizActivity) 具有主页项,第二个活动 (menu2) 具有统计项。 Menu3 有关于项目。现在我已经在导航抽屉的 Menu2 中通过单击项目 home 进行了设置 --> QuizActivity 将打开。现在我还想,当我单击关于按钮时,Menu3 将打开。但不是Menu3,而是我在打开之前使用的最近的应用程序/它会打开应用程序的最近页面,例如测验活动。我有什么错?我还检查了其他类似的线程,但找不到帮助。

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


<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">


    <include
        layout="@layout/navigation_action"
        android:layout_
        android:layout_ />


    <RelativeLayout
        android:layout_
        android:layout_>




        <TextView
            android:id="@+id/textScore2"
            android:layout_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
            android:layout_
            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_
    android:layout_
    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>

菜单3 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);



 

菜单 3 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_
      android:layout_
      tools:context="amapps.impossiblequiz.Menu3"
      android:id="@+id/drawerLayout3">


<android.support.design.widget.NavigationView
    android:layout_
    android:layout_
    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>

【问题讨论】:

在此处添加 menu3 活动以检查是否有任何问题,谢谢。 我添加了 menu3 活动先生 就像我说的你必须在 3 个地方有相同的代码,检查你的 onNavigationItemSelected 方法。但这是不好的做法,如果你想正确地做到这一点,你应该按照我给你的教程做 我现在不想用片段来解决它...首先我想尝试用我的代码解决它,@BrunoFerreira 正在帮助我做到这一点。 【参考方案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;
     

【讨论】:

你好,我一定会试试这个答案,明天......如果它有效,我投票给你先生! 每次单击项目时,意图值都会更改。那么这应该如何改变呢? 因此,他第二次单击导航项时的意图值可能不会改变,使用开关盒通过不改变意图值而是启动我解释为@Nenco 的新意图来避免这种情况switch case 是一种更好的方法,因为它没有效率。 我同意开关是更好的方法,但它仍然不会改变任何事情伙伴:) 我要求放置 menu3 活动以查看它是否有问题,之后如果我发现其他问题,我将更新我的答案。我想帮助他更好地改进应用程序并解决问题:) @Nenco【参考方案2】:

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

更好的方法是使用片段。

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

还要检查 MVP 设计模式。

【讨论】:

以上是关于通过单击导航抽屉上的项目打开虚假活动的主要内容,如果未能解决你的问题,请参考以下文章

单击导航抽屉的项目不会打开片段

单击工具栏上的汉堡包图标不会打开导航抽屉

在导航抽屉项目单击时,导航抽屉中的新列表视图

多个活动的导航抽屉,无需选择导航抽屉活动

android - 通过单击应用程序图标打开导航抽屉?

如何通过android上的复选框控制导航抽屉项目