我创建了两个类。我想在 1 类(MainActivity)上有一个按钮,当它被按下时,它将带我到 2 类(字母)。我尝试了很多方法,但都没有成功。下面是我的原始代码。谁能帮我?对不起,我是应用程序开发的新手。
package com.example.lullabymain;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// new code
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
}
public void onClick(View v) {
int resId = 0;
switch (v.getId()) {
case R.id.button1: resId = R.raw.rockabye; break;
case R.id.button2: resId = R.raw.hushlittlebaby; break;
case R.id.button3: resId = R.raw.twinkle; break;
case R.id.button4: resId = R.raw.hickory; break;
case R.id.button5: resId = R.raw.oldmcd; break;
}
//release any resources from previous mediaplayer
if (mp != null) {
mp.release();
}
//create a new mediaplayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
@Override
protected void onStop()
{
//stop audio
super.onStop();
mp.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
下面的代码是我尝试的代码,其中包括“意图”
package com.example.lullabymain;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// new code
**View button6 = findViewById(R.id.button6);
button6.setOnClickListener(this);**
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
}
public void onClick(View v) {
int resId = 0;
switch (v.getId()) {
case R.id.button1: resId = R.raw.rockabye; break;
case R.id.button2: resId = R.raw.hushlittlebaby; break;
case R.id.button3: resId = R.raw.twinkle; break;
case R.id.button4: resId = R.raw.hickory; break;
case R.id.button5: resId = R.raw.oldmcd; break;
**case R.id.button6:
Intent i = new Intent(this, Alphabet.class);
startActivity(i);
break;**
}
//release any resources from previous mediaplayer
if (mp != null) {
mp.release();
}
//create a new mediaplayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
@Override
protected void onStop()
{
//stop audio
super.onStop();
mp.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}