无法解决'R'..不确定为什么会发生这种情况? Android Studio HW

时间:2016-10-14 01:33:21

标签: java android

enter image description here 目标:播放奶牛和猪的动物声音

  1. 打开的屏幕显示一个农场的img和标题,动物的声音六秒

  2. 第二个屏幕将在牛和猪上显示img,其下方有各自的播放按钮。

  3. 按下一个按钮时,另一个按钮和图像消失,反之亦然。

  4. 按下按钮时,将播放各自的声音

    package net.androidbootcamp.animalvoices;
    
    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        Button btnCow, btnPig;
        MediaPlayer mpPig, mpCow;
        int playing;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnPig = (Button)findViewById(R.id.btnPig);
            btnCow = (Button)findViewById(R.id.btnCow);
            btnPig.setOnClickListener(bPig);
            btnCow.setOnClickListener(bCow);
            mpPig = new MediaPlayer();
            mpPig = MediaPlayer.create(this, R.raw.pigs);
            mpCow = new MediaPlayer();
            mpCow = MediaPlayer.create(this, R.raw.cows);
            playing = 0;
        }
        Button.OnClickListener bPig = new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (playing) {
                    case 0:
                        mpPig.start();
                        playing = 1;
                        btnCow.setText("Pause the Cows");
                        btnPig.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        mpPig.pause();
                        playing = 0;
                        btnCow.setText("Listen to the Cows");
                        btnPig.setVisibility(View.VISIBLE);
                        break;
                }
            }
        };
        Button.OnClickListener bCow = new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (playing) {
                    case 0:
                        mpCow.start();
                        playing = 1;
                        btnCow.setText("Pause the Pigs");
                        btnPig.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        mpCow.pause();
                        playing = 1;
                        btnCow.setText("Listen to the Pigs");
                        btnPig.setVisibility(View.VISIBLE);
                }
            }
        };
    }
    

0 个答案:

没有答案