2014年2月17日 星期一

[Android]My First App Source Code

hair dryer
link : https://play.google.com/store/apps/details?id=com.joe.fantahairdryer


MainActivity.java
package com.joe.fantahairdryer;

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

public class MainActivity extends Activity {
    private  void stopPlaying() {
          if (play !=null){
            play.stop();
            play.release();
            play = null;
        }
    
    }
    private Button btn1, btn2, btn3, stop;
    private MediaPlayer play;
    
    @Override
    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btn1 =    (Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() 
        {        
            @Override
            public void onClick(View v) 
            {
             stopPlaying();
             LinearLayout  linearLayout = (LinearLayout) findViewById(R.id.container);
             linearLayout.setBackgroundResource(R.drawable.btn1);            
                play = MediaPlayer.create(MainActivity.this, R.raw.btn1);
                play.setLooping(true);                
                   if(play.isPlaying() == false)
                    {
                        play.start();  
                    }
                    else
                    {
                        play.stop();
                    }
            }

                        
        });
        btn2 =    (Button)findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() 
        {        
            @Override
            public void onClick(View v) 
            {
                stopPlaying();
                 LinearLayout  linearLayout = (LinearLayout) findViewById(R.id.container);
                 linearLayout.setBackgroundResource(R.drawable.btn2);
                play = MediaPlayer.create(MainActivity.this, R.raw.btn2);
                play.setLooping(true);     
                if(play.isPlaying() == false)
                    {
                        play.start();  
                    }
                    else
                    {
                        play.stop();
                    }
            }
                        
        });
        btn3 =    (Button)findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() 
        {        
            @Override
            public void onClick(View v) 
            {
                stopPlaying();
                 LinearLayout  linearLayout = (LinearLayout) findViewById(R.id.container);
                 linearLayout.setBackgroundResource(R.drawable.btn3);
                play = MediaPlayer.create(MainActivity.this, R.raw.btn3);
                play.setLooping(true);    
                if(play.isPlaying() == false)
                    {
                        play.start();  
                    }
                    else
                    {
                        play.stop();
                    }
            }
                        
        });
        stop =    (Button)findViewById(R.id.stop);
        stop.setOnClickListener(new View.OnClickListener() 
        {        
            @Override
            public void onClick(View v) 
            {            
                play.stop();

            }
                        
        });


    }
    @Override
    protected void onDestroy() {
        if(play !=null && play.isPlaying()) {
            play.stop();
            play.release();
            play = null;
        }
        super.onDestroy();
    }
    @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;
    }

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="bottom"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="STOP" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hair Dryer1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hair Dryer2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hair Dryer3" />
    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxxxxxx"
        ads:loadAdOnCreate="true"
        android:gravity="bottom" >
    </com.google.ads.AdView>




</LinearLayout>

This is layout

沒有留言:

張貼留言