我想随机显示 5 个 xml 布局,并且我会在单击每个出现的 xml 文件的相应按钮时将它们随机播放。到目前为止,下面的代码工作得非常好。---- **我在 xml 文件的每次随机播放上都设置了 0.75 秒的延迟。在这个延迟中,我有声音可以播放。我真的不知道如何实现这一点.在下面的代码中,我使用了 Switch-Case ,但它不起作用。我的要求是如果我单击 xml file1 上的按钮,我想播放 Sound1,如果 Button 来自 Xml 文件 5,我想播放相应的 Sound5 等等....像这样持续 0.75 秒并移动到下一个 XML 文件并重复循环。* ----
public class ReceivingActivity extends Activity{
Random random = new Random();
Handler handler = new Handler();
private int mPosition = 0;
private Handler mHandler = new Handler();
private List<Integer> mLayouts;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mLayouts = new ArrayList<Integer>();
mLayouts.add(R.layout.first);
mLayouts.add(R.layout.second);
mLayouts.add(R.layout.third);
mLayouts.add(R.layout.fourth);
mLayouts.add(R.layout.fifth);
Collections.shuffle(mLayouts);
setContentView(mLayouts.get(mPosition));
mButton = (Button)findViewById(R.id.btnid);
mButton.setOnClickListener(mListener);
mPosition++;
}
private OnClickListener mListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "WTH is happening", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
if(mPosition >= mLayouts.size()){
int lastId = mLayouts.get(mLayouts.size() - 1);
Collections.shuffle(mLayouts);
while (lastId == mLayouts.get(0)) {
Collections.shuffle(mLayouts);
}
mPosition = 0;
}
setContentView(mLayouts.get(mPosition));
switch (mLayouts.get(mPosition)) {
case R.layout.first:
Toast.makeText(getApplicationContext(), "First", Toast.LENGTH_SHORT).show();
iv1.setBackgroundResource(R.drawable.ic_launcher);
break;
case R.layout.second:
Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT).show();
//Some sound
iv2.setBackgroundResource(R.drawable.ic_launcher);
break;
case R.layout.third:
Toast.makeText(getApplicationContext(), "third", Toast.LENGTH_SHORT).show();
//Some media , Different for each Layout..
iv3.setBackgroundResource(R.drawable.ic_launcher);
break;
case R.layout.fourth:
Toast.makeText(getApplicationContext(), "fourth", Toast.LENGTH_SHORT).show();
iv4.setBackgroundResource(R.drawable.ic_launcher);
break;
case R.layout.fifth:
Toast.makeText(getApplicationContext(), "fifth", Toast.LENGTH_SHORT).show();
iv5.setBackgroundResource(R.drawable.ic_launcher);
break;
default:
break;
}
mPosition++;
mButton = (Button)findViewById(R.id.btnid);
mButton.setOnClickListener(mListener);
}
}, 2050);
}
};
mPosition++;
mButton = (Button)findViewById(R.id.btnid);
mButton.setOnClickListener(mListener);
}
}, 750);
}
};
}
任何形式的帮助都非常有帮助。