这是我的第一个合适的应用程序,它工作得很好,但只是想知道谁能缩短我的代码并减少重复性?这是我的代码,它用于音板:
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