作者:HE-KILL-MY-EGO | 来源:互联网 | 2023-08-25 16:49
我有声音和下一个活动的代码。我不知道如何合并这两个代码,以便同时或按顺序执行它们。
package com.example.testmessages;
import androidx.appcompat.app.AppCompatactivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class Mainactivity extends AppCompatactivity {
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add sound
final MediaPlayer gunSoundMP = MediaPlayer.create(this,R.raw.gunsound);
ImageButton GunSound=(ImageButton)this.findViewById(R.id.imageButton3);
GunSound.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
gunSoundMP.start();
}
});
//Image button to execute new activity
imageButton=(ImageButton) findViewById(R.id.imageButton3);
imageButton.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
Intent intentLoadMain2activity = new Intent(Mainactivity.this,Main2activity.class);
startactivity(intentLoadMain2activity);
gunSoundMP.start();
}
});
}
}