Button 在一个活动上使用两个按钮

Button 在一个活动上使用两个按钮,button,if-statement,android-activity,Button,If Statement,Android Activity,我对android还是新手。我有一本儿童读物,我用两个按钮“读给我听”和“读给我自己听” Read to me onclick在阅读本书时会播放录音,但会转到SoundOne活动。 自述会显示文本,并导致第一页活动 我如何创建某种“如果语句”,这样当我单击“读给我”时,它将播放录音,但会导致第一页活动。当我点击自述时,它会显示文本,但仍然会导致第一页活动 这可能有助于减少到目前为止我创建的类的数量,并避免我假设的错误。一些源代码或教程将有助于提前感谢我的代码如下: package com.ine

我对android还是新手。我有一本儿童读物,我用两个按钮“读给我听”和“读给我自己听”

Read to me onclick在阅读本书时会播放录音,但会转到SoundOne活动。 自述会显示文本,并导致第一页活动

我如何创建某种“如果语句”,这样当我单击“读给我”时,它将播放录音,但会导致第一页活动。当我点击自述时,它会显示文本,但仍然会导致第一页活动

这可能有助于减少到目前为止我创建的类的数量,并避免我假设的错误。一些源代码或教程将有助于提前感谢我的代码如下:

package com.inerds.donkiejoukie;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.content.Context;

public class Fbone extends MainActivity {
    MediaPlayer one;
    MediaPlayer mb;
    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbone);
        one = MediaPlayer.create(this, R.raw.pageone);
        mb = MediaPlayer.create(this, R.raw.menubar);
        mp = MediaPlayer.create(this, R.raw.pageflip);
        ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
        imageButton1.setOnClickListener (new OnClickListener() {
            public void onClick(View vone) {
                mb.pause();
                mb.stop();
                mp.start();
                startActivity(new Intent(getApplicationContext(), PageOne.class));
                finish();
            }

        });
        ImageButton readtome = (ImageButton) findViewById(R.id.readtome);
        readtome.setOnClickListener (new OnClickListener() {
            public void onClick(View v) {
                mb.pause();
                mb.stop();
                one.start();
                startActivity(new Intent(getApplicationContext(), SoundOne.class));
                AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
                finish();
            }

        });
    }
}

Intent
机制用于从一个
活动
到另一个
活动的通信。在
Intent
以及要启动的类中,可以指定要执行的操作的名称以及其他选项。在这种情况下,您可能需要使用一个额外的命令来告诉下面的类是否播放声音。当您启动意图时:

Intent nextActivity = new Intent(getApplicationContext(), SoundOne.class);
// Put true or false in the next line according to which click handler you're in.
nextActivity.putBooleanExtra(SoundOne.extra_enableSound, true);
startActivity(nextActivity);
然后在您的
SoundOne
活动中,您需要一个常量字段作为额外名称:

static final String extra_enableSound = "enableSound";
您可以从您的
onCreate
,或任何您想要启动声音的地方,找到这个额外的值:

if (getIntent().getBooleanExtra(extra_enableSound, false)) {
    // start the sound
}

现在您的
PageOne
活动未使用,可以删除。告诉您可以在其中保存哪些其他信息。

我不是专业程序员,因此这可能不是最好的方法,但它对我来说非常有效,只需很少的代码

这是我正在做的

为我的XML中的按钮设置一个“onClick”

然后使用sendmessage而不是OnButton单击

public void sendMessage(View view) 
{
    Intent intent = new Intent(this, ThirdActivity.class);
    startActivity(intent);
}
public void sendMessage1(View view) 
{
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

这是什么样的答案?如果此答案解决了您的问题,请单击左侧的绿色复选标记将其标记为正确。这是表示感谢的堆栈溢出方式,它让其他访问者知道问题已经解决。
 <Button
    android:id="@+id/button2"
    android:layout_width="144dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/button1"
    android:onClick="sendMessage1"
    android:text="@string/cancel"
     />
    Button button1;
Button button2;
public void sendMessage(View view) 
{
    Intent intent = new Intent(this, ThirdActivity.class);
    startActivity(intent);
}
public void sendMessage1(View view) 
{
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}