好的,我試圖簡化我的應用程序中的一些代碼。它工作正常,我只是有點強迫症,並希望不斷提高性能。刪除多個開關/案例陳述
這裏是有問題的代碼看起來像現在:
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote40));
break;
}
switch(ressound){
case R.id.button900:
ressound = R.raw.sound900;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote900));
break;
}
switch(ressound){
case R.id.button901:
ressound = R.raw.sound901;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote901));
break;
}
這是一個音板的應用程序,而這對於保存爲它的功能。有什麼方法可以簡化這些多個語句(某些屏幕有40多種聲音)?使用循環看起來像是一個明顯的選擇,但環顧四周後,case語句顯然必須是靜態的,而不是變量。
編輯:忘了包括實際的函數頭:
public boolean function1(int ressound){
String soundname = "";
做ü這裏有任何錯誤? – Sam
不,它在我的代碼中正常工作。我只是不知道如何製作,所以對於每個按鈕我都不必做出單獨的聲明 – user2328381