gpt4 book ai didi

java - Android Studio 中的数组 for 循环

转载 作者:行者123 更新时间:2023-12-01 08:50:18 26 4
gpt4 key购买 nike

我正在尝试做一些简单的应用程序,例如单击按钮,播放声音。在我的上一个应用程序中,我用开关盒完成了这一切,但我意识到,如果我有很多代码,我必须为每个按钮一个案例编写大量代码。现在我试图将我的按钮和声音保存到数组中,并以 double for 循环它们。

我还做了一些工作正常的上下文菜单。但我现在的问题是播放声音。实际上它会为两个按钮播放声音。它应该为button1 -> sound1 和button2 -> sound2 播放

有人可以在这里找到我的问题吗?

public class MainActivity extends AppCompatActivity {

MediaPlayer MainMedia;

public int [] buttonsas = {R.id.button1, R.id.button2};
public int [] sounds = {R.raw.sound1, R.raw.sound2};

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

for (int i=0; i<buttonsas.length; i++) {
Button contextMenuButton = (Button) findViewById(buttonsas[i]) ;
registerForContextMenu(contextMenuButton);
}

MainMedia = MediaPlayer.create(this, R.raw.sound1);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_option1:
case R.id.item_option2:
Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show();
break;
case R.id.item_option3:
Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show();
break;
}

return super.onContextItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.main_menu, menu);
}

public void MainMedia (View view) {
for (int i=0; i<buttonsas.length; i++) {
for (int j=0; j<sounds.length; j++) {
MainMedia.release();
MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]);
MainMedia.start();
}
}
}
}

最佳答案

如果我正确理解你的代码,在方法 MainMedia 中,它IS实际上为两个按钮播放两种声音,但它看起来只是尽管它正在播放第二个,因为第一个几乎立即发布。

老实说,我认为有更简洁、更少处理的解决方案,但如果你想用两个嵌套的 for 来实现,那么我认为解决方案可能是:

尝试在第二个 for 之后添加 if 语句,以便您知道您按下的是哪个按钮:

public void MainMedia (View view) {
for (int i=0; i<buttonsas.length; i++) {
for (int j=0; j<sounds.length; j++){
if (view.getId() == buttonsas[i] && i == j) {
MainMedia.release();
MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]);
MainMedia.start();
}
}
}
}

虽然我认为这个解决方案应该有效,但我建议做这样的事情,更好:

public void MainMedia (View view) {
// Traverse the buttonsas array to get the item the user just pressed.
for (int i=0; i<buttonsas.length; i++) {
// Check whether the item we got from the array is equal to the item we received as a parameter
if (view.getId() == buttonsas[i]) {
MainMedia.release();

// Since the sounds and buttons have a relation in the same position in two different arrays,
// just get the position we have depending on the selected item.
MainMedia = MediaPlayer.create(MainActivity.this, sounds[i]);
MainMedia.start();
}
}
}

这样你就可以保存一个for循环。

请告诉我这是否有帮助。

编辑:我在代码中添加了注释,以便更容易理解。简而言之,它之所以有效,是因为在两个单独的数组中存在 item - sound 关系,因此 item1 (R.id.button1)位于 array1 (buttonsas) 的 position1 (buttonsas[1]) 中,这样,当我得到我想要的item位置,我只是在array2上使用相同的位置(声音[ 1])。由于您使用的是迭代器(for 循环),因此位置是 i

关于java - Android Studio 中的数组 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42435756/

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