在一个活动使用两个按钮 [英] Using two Buttons on one Activity

查看:124
本文介绍了在一个活动使用两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是新到Android。我有一个孩子预定使L做有两个按钮读给我听和读给我。

读给我听的onclick,因为它读取本书将播放录音,但它去SoundOne活动。 阅读对自己将显示文本,并导致PAGEONE活动。

如何l创建某种形式的if语句,使得当L点击阅读对我来说会播放录音,但导致PAGEONE活动。而当升点击阅读到自己也能显示文字,但仍然会导致PAGEONE活动

这可能有助于减少l具备创建至今类的数量,避免ANR升承担。某些源$ C ​​$ C或A教程将帮助在此先感谢我的code是如下:

 包com.inerds.donkiejoukie;

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

公共类Fbone扩展MainActivity {
    MediaPlayer的之一;
    MediaPlayer的MB;
    MediaPlayer的熔点;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fbone);
        1 = MediaPlayer.create(这一点,R.raw.pageone);
        MB = MediaPlayer.create(这一点,R.raw.menubar);
        MP = MediaPlayer.create(这一点,R.raw.pageflip);
        的ImageButton imageButton1 =(的ImageButton)findViewById(R.id.imageButton1);
        imageButton1.setOnClickListener(新OnClickListener(){
            公共无效的onClick(查看VONE){
                mb.pause();
                mb.stop();
                mp.start();
                startActivity(新意图(getApplicationContext(),PageOne.class));
                完();
            }

        });
        的ImageButton readtome =(的ImageButton)findViewById(R.id.readtome);
        readtome.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                mb.pause();
                mb.stop();
                one.start();
                startActivity(新意图(getApplicationContext(),SoundOne.class));
                AudioManager audioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,1​​00,0);
                完();
            }

        });
    }
}
 

解决方案

意图机制用于从一个活动的通信到另一个。在意图,还有一类开始,您可以指定执行一个动作的名称,以及其他选项。在这种情况下,你可能想使用的额外的告诉下面的类是否播放声音与否。当您启动的目的:

 意图nextActivity =新的意向书(getApplicationContext(),SoundOne.class);
//根据你在其中单击处理程序将真或假的下一行。
nextActivity.putBooleanExtra(SoundOne.extra_enableSound,真正的);
startActivity(nextActivity);
 

,然后在 SoundOne 的活动,你需要一个恒定的领域,为额外的名称:

 静态最后弦乐extra_enableSound =enableSound;
 

,你可以找到这个额外的值从你的的onCreate ,或任何你想要启动的声音:

 如果(getIntent()。getBooleanExtra(extra_enableSound,FALSE)){
    //启动的声音
}
 

现在你的 PAGEONE 的活动是不使用的,可以删除。该文档意图 告诉你什么其他的信息,您可以在那里藏匿。

I'm still new to android. I have a kids book that l have done with two buttons "READ TO ME" and "READ TO MYSELF".

Read to me onclick will play a recording as it reads the book but it goes to SoundOne Activity. Read to myself will display text and it leads to PageOne Activity.

How do l create some sort of an "if statement" such that when l click read to me it will play the recording but lead to PageOne Activity. And when l click read to myself it will display the text but still leads to PageOne Activity

This might help reduce the number of classes l have created so far and avoid ANR l assume. Some source code or a tutorial will help Thanks in advance my code is below:

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();
            }

        });
    }
}

解决方案

The Intent mechanism is used to communicate from one Activity to another. In the Intent, as well as a class to start, you can specify the name of an action to perform, as well as other options. In this case, you might want to use an extra to tell the following class whether to play sound or not. When you start the 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);

and then in your SoundOne activity, you need a constant field for the extra name:

static final String extra_enableSound = "enableSound";

and you can find the value of this extra from your onCreate, or wherever you want to start the sound:

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

Now your PageOne activity is unused and can be deleted. The documentation for Intent tells you what other information you can stash in there.

这篇关于在一个活动使用两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆