好吧,所以我很久以前就问了这样的问题,但我没有得到我想要的答案。主要可能是因为我没有包含足够的代码。我先粘贴它,然后再解释。
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone/Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Ringtone/Notification") {
function1(item.getItemId());
} else {
return false;
}
return true;
}
// detect button clicked, save as a ringtone using strings.xml
public boolean function1(int ressound) {
String soundname = "";
switch (ressound) {
case R.id.button01:
ressound = R.raw.sound01;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote01));
break;
case R.id.button02:
ressound = R.raw.sound02;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote02));
break;
case R.id.button03:
ressound = R.raw.sound03;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote03));
break;
}
我的问题在于switch语句。最终,有很多按钮/引号/声音都会按照"按钮"之后的数字进行匹配。 "声音"和#34;引用"。在我的上一个应用程序中,一次完成并复制/粘贴多达50个开关盒并且通过并编辑每个特定于button10 / sound10 / quote10,button11 / sound11 / quote11,非常非常繁琐且无聊等等。
我试图玩弄一些东西,但似乎无法让它发挥作用。
如果它有帮助,我将它们全部放在这样的数组中(出于目前代码中其他地方的目的):
final int[] buttonIds = {R.id.button01, R.id.button02, R.id.button03,
R.id.button04, R.id.button05, R.id.button06,
R.id.button07, R.id.button08, R.id.button09,
R.id.button10};
final int[] soundIds = {R.raw.sound01, R.raw.sound02, R.raw.sound03,
R.raw.sound04, R.raw.sound05, R.raw.sound06,
R.raw.sound07, R.raw.sound08, R.raw.sound09,
R.raw.sound10};
final int[] stringIds = {R.string.quote01, R.string.quote02, R.string.quote03,
R.string.quote04, R.string.quote05, R.string.quote06,
R.string.quote07, R.string.quote08, R.string.quote09,
R.string.quote10};
先谢谢你的帮助和指导!