2012-02-20 54 views
0

我创建一个音板。 当我点击一个按钮,在音效档播放和selectedSoundIdsoundIds[i];设置变量后创建contextmenu

如果我长按按钮,按钮之前是“短”按规定,没有定义selectedSoundId。 所以我需要定义它,即使我没有“短按”按钮。

我想通过“onCreateContextMenu”来定义它,但是怎么样?

public class Activity2 extends TabActivity { 

    /** Called when the activity is first created. */ 
    int selectedSoundId; 
    int random = (int)Math.ceil(Math.random()*100); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 

     //Look up the AdView as a resource and load a request. 
     AdView adView = (AdView)this.findViewById(R.id.adView); 
     adView.loadAd(new AdRequest()); 

     TabHost mTabHost = getTabHost(); 

     mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("text1").setContent(R.id.tab1)); 
     mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("text2").setContent(R.id.tab2)); 
     mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("text3").setContent(R.id.tab3)); 
     mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("text4").setContent(R.id.tab4)); 

     mTabHost.setCurrentTab(0); 

     final MediaPlayer player = new MediaPlayer(); 
     final Resources res = getResources(); 

     //just keep them in the same order, 
     final int[] buttonIds = { R.id.button1, R.id.button2,}; 
     final int[] soundIds = { R.raw.sound1, R.raw.sound2, }; 

     View.OnClickListener listener = new View.OnClickListener() { 
      public void onClick(View v) { 
       //find the index that matches the button's ID, and then reset 
       //the MediaPlayer instance, set the data source to the corresponding 
       //sound effect, prepare it, and start it playing. 
       for(int i = 0; i < buttonIds.length; i++) { 
        if(v.getId() == buttonIds[i]) { 
         selectedSoundId = soundIds[i]; 
         AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]); 
         player.reset(); 
         try { 
          player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
         } catch (IllegalArgumentException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         try { 
          player.prepare(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         player.start(); 

         break; 
        } 
       } 
      } 
     }; 
     //set the same listener for every button ID, no need 
     //to keep a reference to every button 
     for(int i = 0; i < buttonIds.length; i++) { 
      Button soundButton = (Button)findViewById(buttonIds[i]); 
      registerForContextMenu(soundButton); 
      soundButton.setOnClickListener(listener); 

     } 



    } 

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

// How do I give SelectedsoundId here the value of soundIds[i] 
//Like : selectedSoundId = soundIds[i]; 

    } 

UPDATE

我在OnCreateContextMenu加入这个代码,我认为它现在正常工作。 我不知道是否需要v.getId();

v.getId(); 
    for(int i = 0; i < buttonIds.length; i++) { 
      if(v.getId() == buttonIds[i]) { 
       selectedSoundId = soundIds[i]; 
      } 
     } 

回答

0

onCreateContextMenu()方法有一个名为View v表示的量,ContextMenu正在修建的View参数。所以从这里你可以得到ID(v.getId()),然后,就像你在View.OnClickListener listener中那样,找出buttonIds数组中的位置,然后相应地设置selectedSoundId

而且这样的:

menu.add(0, v.getId(), 0, "Ringtone"); 

应该是这样的:

menu.add(Menu.NONE, UNIQUE_ID, Menu.NONE, "Ringtone"); 

编辑:

在你加入我看行v.getId();你之前循环的代码,这是没有必要的。 关于我的菜单编辑,您应该为菜单项指定一个唯一的ID,因此在方法onContextItemSelected()中,您可以找出用户选择哪个菜单项(see this class as an example)。在其他领域,你可以使用0或Menu.NONE。

+0

我已经使用新的附加代码更新了帖子,请参阅编辑。 你对菜单编辑有什么意义? – Bldjef 2012-02-20 15:03:52

+0

@Bldjef查看编辑答案。 – Luksprog 2012-02-20 15:20:19