我正在创建一个音板。
当我点击一个按钮时,播放声音文件,selectedSoundId
定义为soundIds[i];
如果我长按按钮,在按下“短按”按钮之前,未定义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];
}
更新
我在OnCreateContextMenu
中添加了此代码,我认为它现在正常运行。
我不确定v.getId();
是否必要?
v.getId();
for(int i = 0; i < buttonIds.length; i++) {
if(v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
}
}
答案 0 :(得分:0)
onCreateContextMenu()
方法有一个名为View v
的参数,代表View
正在构建的ContextMenu
。因此,您可以获取ID(v.getId()
)然后,就像在View.OnClickListener listener
中一样,找出buttonIds
数组中的位置,然后设置selectedSoundId
相应
还有:
menu.add(0, v.getId(), 0, "Ringtone");
应该是这样的:
menu.add(Menu.NONE, UNIQUE_ID, Menu.NONE, "Ringtone");
修改强>
在你添加的代码中,我在你的for循环之前看到了行v.getId();
,这是没有必要的。
关于我的菜单编辑,您应该为菜单项指定一个唯一的ID,因此在方法onContextItemSelected()
中,您可以找到用户选择的菜单项(see this class as an example)。在其他字段中,您可以使用0或Menu.NONE。