如何在一个活动中将弹出菜单添加到多个按钮?

时间:2018-11-23 13:06:11

标签: android android-studio

我在将多个弹出菜单添加到不同的按钮时遇到问题。但是,我配置为向一个设置按钮添加一个弹出菜单,如下面的屏幕快照所示,但是当我将相同的代码应用于其他设置按钮时,应用崩溃。

请告诉我如何为所有不同的设置按钮添加多个弹出菜单。

https://i.stack.imgur.com/u5wjf.jpg

这是我的Java代码

package com.techjapreet.shivshankarkiringtone;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class ringtone_tab extends AppCompatActivity {

    Button ringtonepopup;
    Button ringtonepopup2;
    Button ringtonepopup3;
    Button ringtonepopup4;
    Button ringtonepopup5;
    Button ringtonepopup6;

    Button clk1;
    Button clk2;
    Button clk3;
    Button clk4;
    Button clk5;
    Button clk6;

    MediaPlayer mediaPlayer;

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

        final Button ringtonepopup = (Button) findViewById(R.id.btn_setting1);

        ringtonepopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, ringtonepopup);

                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();

                        return true;
                    }
                });


                popupMenu.show();
            }
        });

        clk6 = (Button) findViewById(R.id.btn_play6);

        clk5 = (Button) findViewById(R.id.btn_play5);

        clk4 = (Button) findViewById(R.id.btn_play4);

        clk3 = (Button) findViewById(R.id.btn_play3);

        clk2 = (Button) findViewById(R.id.btn_play2);

        clk1 = (Button) findViewById(R.id.btn_play1);

        mediaPlayer = new MediaPlayer();

    }



    public void setBtn_play6(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
        mediaPlayer.start();

    }

    public void setBtn_play5(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
        mediaPlayer.start();

    }



    public void setBtn_play4(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
        mediaPlayer.start();

    }

    public void setBtn_play3(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
        mediaPlayer.start();

    }



    public void setBtn_play2(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
        mediaPlayer.start();


    }


    public void setBtn_play1(View v)
    {
        stopPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
        mediaPlayer.start();
    }

    private void stopPlayer(){
        if(mediaPlayer != null && mediaPlayer.isPlaying())
        {mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
        }
    }

}

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play1"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play1" />


        <Button
            android:id="@+id/btn_setting1"
            android:onClick="setsetting1"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:layout_toStartOf="@+id/btn_play1"
            android:layout_toLeftOf="@+id/btn_play1"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item1"
            android:layout_alignBottom="@+id/txt_grid_item1"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="2dp"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginRight="4dp"
            android:layout_toStartOf="@+id/btn_setting1"
            android:layout_toLeftOf="@+id/btn_setting1"
            android:layout_toEndOf="@+id/img_grid_item1"
            android:layout_toRightOf="@+id/img_grid_item1"
            android:gravity="center_vertical"
            android:text="@string/textview"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play2"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play2"/>


        <Button
            android:id="@+id/btn_setting2"
            android:onClick="setsetting2"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:layout_toStartOf="@+id/btn_play2"
            android:layout_toLeftOf="@+id/btn_play2"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item2"
            android:layout_alignBottom="@+id/txt_grid_item2"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo1"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toStartOf="@+id/btn_setting2"
            android:layout_toLeftOf="@+id/btn_setting2"
            android:layout_toEndOf="@+id/img_grid_item2"
            android:layout_toRightOf="@+id/img_grid_item2"
            android:gravity="center_vertical"
            android:text="@string/textview1"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout3"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play3"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play3"/>


        <Button
            android:id="@+id/btn_setting3"
            android:onClick="setsetting3"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:layout_toStartOf="@+id/btn_play3"
            android:layout_toLeftOf="@+id/btn_play3"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item3"
            android:layout_alignBottom="@+id/txt_grid_item3"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo2"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toStartOf="@+id/btn_setting3"
            android:layout_toLeftOf="@+id/btn_setting3"
            android:layout_toEndOf="@+id/img_grid_item3"
            android:layout_toRightOf="@+id/img_grid_item3"
            android:gravity="center_vertical"
            android:text="@string/textview2"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout4"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play4"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play4"/>


        <Button
            android:id="@+id/btn_setting4"
            android:onClick="setsetting4"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:layout_toStartOf="@+id/btn_play4"
            android:layout_toLeftOf="@+id/btn_play4"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item4"
            android:layout_alignBottom="@+id/txt_grid_item4"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo3"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toStartOf="@+id/btn_setting4"
            android:layout_toLeftOf="@+id/btn_setting4"
            android:layout_toEndOf="@+id/img_grid_item4"
            android:layout_toRightOf="@+id/img_grid_item4"
            android:gravity="center_vertical"
            android:text="@string/textview3"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout5"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play5"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play5"/>


        <Button
            android:id="@+id/btn_setting5"
            android:onClick="setsetting5"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:layout_toStartOf="@+id/btn_play5"
            android:layout_toLeftOf="@+id/btn_play5"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item5"
            android:layout_alignBottom="@+id/txt_grid_item5"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo4"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toStartOf="@+id/btn_setting5"
            android:layout_toLeftOf="@+id/btn_setting5"
            android:layout_toEndOf="@+id/img_grid_item5"
            android:layout_toRightOf="@+id/img_grid_item5"
            android:gravity="center_vertical"
            android:text="@string/textview4"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout6"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/btn_play6"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/ic_play"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="setBtn_play6"/>


        <Button
            android:id="@+id/btn_setting6"
            android:onClick="setsetting6"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:layout_toStartOf="@+id/btn_play6"
            android:layout_toLeftOf="@+id/btn_play6"
            android:background="@drawable/ic_setting"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:src="@drawable/ic_setting" />

        <ImageView
            android:id="@+id/img_grid_item6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txt_grid_item6"
            android:layout_alignBottom="@+id/txt_grid_item6"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/todo5"
            android:gravity="center_vertical"
            android:src="@drawable/ic_logo"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_grid_item6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toStartOf="@+id/btn_setting6"
            android:layout_toLeftOf="@+id/btn_setting6"
            android:layout_toEndOf="@+id/img_grid_item6"
            android:layout_toRightOf="@+id/img_grid_item6"
            android:gravity="center_vertical"
            android:text="@string/textview5"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />


    </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

定义一个听众:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // place your code here
        switch (v.getId()) {
            case R.id.btn_play1:
                PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, v);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                popupMenu.show();
                break;
            case R.id.btn_play2:
                // place your code here
                break;
        }
    }
};

并将此侦听器应用于所有按钮:

clk1.setOnClickListener(listener);
clk2.setOnClickListener(listener);
.................................