我需要帮助在 android 上制作长按上下文菜单

发布于 2024-10-15 07:48:08 字数 1176 浏览 2 评论 0原文

好吧,现在读完后我确实有一个像我想要的那样长按的菜单......唯一的问题是它实际上没有获取声音文件并保存它

我想知道我现在做错了什么?这是我使用的代码:

Button SoundButton1 = (Button) findViewById(R.id.money);  
        registerForContextMenu(SoundButton1);
    }

     @Override  
        public void onCreateContextMenu(ContextMenu menu, View v, 
                ContextMenuInfo menuInfo) { 
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.setHeaderTitle("Save as..."); 
            menu.add(0, MENU_RINGTONE, 0, "Ringtone"); 
            menu.add(0, MENU_NOTIFICATION, 0, "Notification"); 
    } 

     @Override  
        public boolean onContextItemSelected(MenuItem item) {  
            if(item.getTitle()=="Ringtone"){function1(item.getItemId());}  
            else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
            else {return false;}  
        return true;  
        }  

        public void function1(int id){  
            Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();  
        }  
        public void function2(int id){  
            Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();  
        }

Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it

I am wondering what did I do wrong now? Here is the code I used:

Button SoundButton1 = (Button) findViewById(R.id.money);  
        registerForContextMenu(SoundButton1);
    }

     @Override  
        public void onCreateContextMenu(ContextMenu menu, View v, 
                ContextMenuInfo menuInfo) { 
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.setHeaderTitle("Save as..."); 
            menu.add(0, MENU_RINGTONE, 0, "Ringtone"); 
            menu.add(0, MENU_NOTIFICATION, 0, "Notification"); 
    } 

     @Override  
        public boolean onContextItemSelected(MenuItem item) {  
            if(item.getTitle()=="Ringtone"){function1(item.getItemId());}  
            else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
            else {return false;}  
        return true;  
        }  

        public void function1(int id){  
            Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();  
        }  
        public void function2(int id){  
            Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();  
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-10-22 07:48:08

您没有正确比较Strings。此测试:

if(item.getTitle()=="Ringtone")

检查 getTitle() 返回的对象是否与“Ringtone”相同的对象。这将始终为false。与 else if 测试类似。您实际上想要将 getTitle与您的String进行比较。您应该将它们替换为 String#equals() 方法。这是代码:

if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}  
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}  
else {return false;}

You are not comparing Strings properly. This test:

if(item.getTitle()=="Ringtone")

checks if the object returned by getTitle() is the same object as "Ringtone". This will always be false. Similarly with the else if test. You actually want to compare the value of getTitle with your String. You should replace them with the String#equals() method. Here is the code:

if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}  
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}  
else {return false;}
陈甜 2024-10-22 07:48:08

如果您使用的是 ListFragment 那么您还需要做

getListView().setOnCreateContextMenuListener(this);

而不是

registerForContextMenu(listView);

看起来。

If you are using a ListFragment then you also need to do

getListView().setOnCreateContextMenuListener(this);

instead of

registerForContextMenu(listView);

it seems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文