2013-09-21 49 views
0

這是我的第一個正確的應用程序,它工作的很好,但只是好奇看看誰能縮短我的代碼並使其重複性更低?這裏是我的代碼,它是一個音板:替代重複代碼行(Android/Java)

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    //Tell system to use Media Volume rather than Ringer 

    setVolumeControlStream(AudioManager.STREAM_MUSIC); 

    // Button references 

    Button button1 = (Button) findViewById(R.id.button1); 
    Button button2 = (Button) findViewById(R.id.button2); 
    Button button3 = (Button) findViewById(R.id.button3); 
    Button button4 = (Button) findViewById(R.id.button4); 
    Button button5 = (Button) findViewById(R.id.button5); 
    Button button6 = (Button) findViewById(R.id.button6); 

    Button button8 = (Button) findViewById(R.id.button8); 
    Button button9 = (Button) findViewById(R.id.button9); 
    Button button10 = (Button) findViewById(R.id.button10); 
    Button button11 = (Button) findViewById(R.id.button11); 
    Button button12 = (Button) findViewById(R.id.button12); 
    Button button13 = (Button) findViewById(R.id.button13); 


    // Button Sounds to be used by onClickListener 

    final MediaPlayer buttonSound1 = MediaPlayer.create(MainActivity.this, 
      R.raw.afternoondelight); 
    final MediaPlayer buttonSound2 = MediaPlayer.create(MainActivity.this, 
      R.raw.alrightythen); 
    final MediaPlayer buttonSound3 = MediaPlayer.create(MainActivity.this, 
      R.raw.ballsshowing); 
    final MediaPlayer buttonSound4 = MediaPlayer.create(MainActivity.this, 
      R.raw.blackmen); 
    final MediaPlayer buttonSound5 = MediaPlayer.create(MainActivity.this, 
      R.raw.doh); 
    final MediaPlayer buttonSound6 = MediaPlayer.create(MainActivity.this, 
      R.raw.fxxk); 
    final MediaPlayer buttonSound8 = MediaPlayer.create(MainActivity.this, 
      R.raw.mclovin); 
    final MediaPlayer buttonSound9 = MediaPlayer.create(MainActivity.this, 
      R.raw.pacmandeath); 
    final MediaPlayer buttonSound10 = MediaPlayer.create(MainActivity.this, 
      R.raw.quickwhite); 
    final MediaPlayer buttonSound11 = MediaPlayer.create(MainActivity.this, 
      R.raw.sexylady); 
    final MediaPlayer buttonSound12 = MediaPlayer.create(MainActivity.this, 
      R.raw.troll); 
    final MediaPlayer buttonSound13 = MediaPlayer.create(MainActivity.this, 
      R.raw.turd); 

    //onClickListeners, button7 and button14 have been removed due to explicit content. 

    button1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      buttonSound1.start(); 

     } 
    }); 
    button2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound2.start(); 

     } 
    }); 
    button3.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      buttonSound3.start(); 

     } 
    }); 
    button4.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound4.start(); 

     } 
    }); 
    button5.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound5.start(); 

     } 
    }); 
    button6.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound6.start(); 
     } 
    }); 
    //Where button7 was 
    button8.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound8.start(); 
     } 
    }); 
    button9.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound9.start(); 
     } 
    }); 
    button10.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound10.start(); 
     } 
    }); 
    button11.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound11.start(); 
     } 
    }); 
    button12.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound12.start(); 
     } 
    }); 
    button13.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      buttonSound13.start(); 

     } 
    }); 
    //Where button14 was 
+0

oh ya那麼控制數組如何簡單,因爲它可能是 – Trikaldarshi

+2

[This](http://codereview.stackexchange.com/)是這類問題的更好的地方 –

回答

0

對於初學者來說,你應該只初始化一個MediaPlayer的爲類的屬性和點擊設置資源發揮和真正發揮它。您可以創建一個onClickListener,並通過點擊按鈕選擇聲音文件。希望有所幫助

1

你不需要每個按鈕的顯式點擊監聽器。讓Actitvity實現監聽器並處理onClick中的點擊。此外,將MediaPlayer作爲字段使用,因爲不需要創建14個單獨的引用。

public class MainActivity extends Activity implements View.OnClickListener { 

    MediaPlayer sound; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     //Tell system to use Media Volume rather than Ringer 

     setVolumeControlStream(AudioManager.STREAM_MUSIC); 

     // Button references 

     Button button1 = (Button) findViewById(R.id.button1); 
     Button button2 = (Button) findViewById(R.id.button2); 
     Button button3 = (Button) findViewById(R.id.button3); 
     Button button4 = (Button) findViewById(R.id.button4); 
     Button button5 = (Button) findViewById(R.id.button5); 
     Button button6 = (Button) findViewById(R.id.button6); 

     Button button8 = (Button) findViewById(R.id.button8); 
     Button button9 = (Button) findViewById(R.id.button9); 
     Button button10 = (Button) findViewById(R.id.button10); 
     Button button11 = (Button) findViewById(R.id.button11); 
     Button button12 = (Button) findViewById(R.id.button12); 
     Button button13 = (Button) findViewById(R.id.button13); 

     // assign the click listener to the button 

     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     // etc 
} 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()) 
     case R.id.button1: 
      sound = MediaPlayer.create(MainActivity.this, 
        R.raw.afternoondelight); 
      sound.start(); 
      break; 

     case R.id.button2: 
      sound = MediaPlayer.create(MainActivity.this, 
        R.raw.alrightythen); 
      sound.start(); 
      break; 

     // and so on 
    } 
} 

這應該有助於開始。

+0

啊好吧我很酷我明白了工作,歡呼聲:) –

0

既然你是新的節目在所有的,我希望你學習常規;) 你的代碼可以在常規縮短這樣的:

def effects = ["afternoondelight", "alrightythen", "ballsshowing", "blackmen", "doh", "fxxk", "mclovin", "pacmandeath", "quickwhite", "sexylady", "troll", "turd"] 

effects.size().times { 
Button button = (Button) findViewById(R.id."button${it}"); 
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw."${effects[it]}"); 
button.onClickListener = [onClick: { View v -> buttonSound.start() } as View.OnClickListener] 
} 
+0

Groovy在Android上還沒有... – assylias

0

你的代碼結構良好。但你是對的,像這樣複製是邪惡的。這種方法既醜陋又容易出錯(當人們感到更無聊時,人們會犯更多錯誤,包括開發人員)。

因此,有幾件事情,你可以/應該做的向下收縮你的代碼:

  • 使用roboguice或butterknife到avoir的findViewById語句。隨着RoboGuice你將宣佈領域,如

    @InjectView(R.id.button1) 
    private Button button1 
    

也不遑多讓,但仍然只是必要的,僅此而已。

  • 一種不同的方法,我只用於MediaPlayers(但可以使用按鈕)將系列放在工廠方法中。

    MediaPlayer buttonSound1 = createPlayerForResource(R.raw.afternoondelight); 
    
  • ,然後你可以使用最後的絕招,我會用它來與聽衆一起結合這一點,但你也可以使用按鈕和數組:將串聯成一個數據結構(數組列表,地圖等)

    Map<Button, Integer> mapButtonToMediaPlayerResourceId = new HashMap<Button, Integer>(); 
    //then init the map 
    mapButtonToMediaPlayerResourceId.put(button1, R.raw.afternoondelight); 
    //and so on 
    

然後你可以使用相同的監聽所有的按鈕:

private class OnClickListenerPlaySoundForbutton { 
    @Override 
    public void onClick(View v) { 
     createPlayerForResource(mapButtonToMediaPlayerResourceId.get((Button) v)).start(); 
    } 
} 

順便說一下,對於11個按鈕,您是否考慮使用ListView?

+0

好吧歡呼,抱歉是一個痛苦,但我需要了解你的建議如何工作,所以你可以喜歡展開他們?我真的很新(所以年輕,所以需要從字面上一切解釋哈哈哈),所以需要看看如何代碼一起工作等。 –

2

有很多方法可以縮短代碼。這裏是其中一些:

  • ,因爲每個按鈕都有其ID設置爲「按鈕#」,你可以使用一個循環,通過使用類似越過所有的人:
int resId = getResources().getIdentifier("button" + i, "id", getPackageName()); 
Button b=findViewById(resId); 

然後,你可以通過在他們每個人的功能設置onClickListener,每次使用一個常量數組在開始設置得到正確的資源爲當前按鈕來播放,例如:

private static final int[] SOUNDS=new 
int[]{R.raw.afternoondelight,R.raw.alrightythen,... }; 
  • 代替上述方法的陣列,可以設置每個在XML按鈕的標籤,以指向正確的聲音。您可以(僅用於按鈕)在XML中設置onClick方法,因此您只需檢查函數中哪些按鈕被按下,並且您選擇了哪個按鈕發出什麼聲音。

  • 另一種選擇是創建一個自定義視圖,該視圖具有播放聲音的屬性,這將允許您在XMl中執行所有操作。

  • 使用第三方庫如RoboGuiceAndroid Query

順便說一句,既然你有這麼多的按鈕,你介意告訴我們他們的位置和其他屬性?也許你可以做一些優化創建它們,而不是在xml文件中有這麼多的項目...也許你可以把它們放在一個gridView或listView中,並避免創建它們很多...

也,請考慮讓mediaPlayer成爲一個單獨的字段,在不需要的時候發佈。原因是爲了消除加載時間和減少內存使用。

+0

我真的只是開始編碼最近,這個程序使用我所知道的一切,所以你的忠告真的幫助:)我這樣設置它,因爲儘管它是詳盡的,但我明白代碼是如何協同工作的,所以有什麼機會可以解釋你的建議是如何工作的,所以我可以在我的腦海中理解它嗎?對不起,年輕的編程noob在這裏:3 –

+0

@NathanHoy好了,那麼你想用哪種方法來解釋一下?這也取決於你真正需要你的應用程序做什麼。現在,我只能猜測它具有發出聲音的按鈕。他們形成一個網格(就像一個矩陣)? –

+0

循環和數組請和是的,這就是它,他們確實形成一個網格:) –