7. Add song to Phone

package com.example.thenewboston;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private int counter;
    private Button add, sub;
    private TextView display;
    private MediaPlayer clickSong;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        counter = 0;
        add = (Button) this.findViewById(R.id.add);
        sub = (Button) this.findViewById(R.id.second);
        display = (TextView)this.findViewById(R.id.tvDiaplay);
        
        //SoundPool
        clickSong = MediaPlayer.create(MainActivity.this, R.raw.mouseclick2);
        
        add.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //when click add 1 to the counter
                clickSong.start();
                counter++;
                display.setText("Your total is "+counter);
            }
        });
        
        sub.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //minus 1 
                clickSong.start();
                counter--;
                display.setText("Your total is "+counter);
            }
        });
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        clickSong.release();
    }
    
    

}

 

posted @ 2013-11-21 05:34  Zhentiw  阅读(133)  评论(0编辑  收藏  举报