gpt4 book ai didi

java - 重复代码行的替代方案(Android/Java)

转载 作者:行者123 更新时间:2023-12-01 23:34:15 24 4
gpt4 key购买 nike

这是我的第一个正确的应用程序,它工作得很好,但我只是想知道谁可以缩短我的代码并减少重复?这是我的代码,它用于音板:

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

最佳答案

有很多方法可以使代码更短。以下是其中一些:

  • 由于每个按钮的 id 设置为“button#”,因此您可以使用如下循环遍历所有按钮:
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 方法,而不是查找 View ,因此您需要做的就是检查函数中按下了哪个按钮,然后您就可以选择要发出的声音。

  • 另一种选择是创建一个自定义 View ,该 View 具有用于播放声音的属性,这将允许您在 XMl 中执行所有操作。

  • 使用第三方库,如 RoboGuiceAndroid Query .

顺便说一句,既然你有这么多按钮,你介意告诉我们它们的位置和其他属性吗?也许您也可以在创建它们时进行一些优化,而不是在 xml 文件中包含如此多的项目...也许您可以将它们放入 gridView 或 listView 中,并避免创建如此多的项目...

另外,请考虑将 mediaPlayer 设为单个字段,在不需要时将其释放。原因是为了减少加载时间并减少使用的内存。

关于java - 重复代码行的替代方案(Android/Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18934845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com