Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android開發教程之高煥堂-上課講義(3)

Android開發教程之高煥堂-上課講義(3)

編輯:初級開發

1.2.2 範例程式(1):採單純繼承方法

1.2.2.1 撰寫步驟:


Step-1: 建立android專案:Px01。

Step-2: 撰寫Activity的子類別:ac01,其程式碼如下:

/* ac01.Java */

package com.misoo.pkzz;

import android.app.Activity;

import android.graphics.Color;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.view.VIEw;

import android.view.VIEw.OnClickListener;

import android.widget.LinearLayout;

import android.widget.TextVIEw;

public class ac01 extends Activity implements OnClickListener {

private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

private final int FP = LinearLayout.LayoutParams.FILL_PARENT;

private MediaPlayer mPlayer;

private myButton btn, btn2, btn3;

public TextVIEw tv;

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

LinearLayout layout = new LinearLayout(this);

layout.setOrIEntation(LinearLayout.VERTICAL);

btn = new myButton(this);

btn.setId(101);

btn.setText("play");

btn.setOnClickListener(this);

LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(btn.get_width(),

btn.get_height());

param.topMargin = 10;

layout.addVIEw(btn, param);

btn2 = new myButton(this);

btn2.setId(102);

btn2.setText("stop");

btn2.setOnClickListener(this);

layout.addVIEw(btn2, param);

btn3 = new myButton(this);

btn3.setId(103);

btn3.setText("exit");

btn3.setOnClickListener(this);

layout.addVIEw(btn3, param);

tv = new TextVIEw(this);

tv.setTextColor(Color.WHITE);

tv.setText("Ready");

LinearLayout.LayoutParams param2 =

new LinearLayout.LayoutParams(FP, WC);

param2.topMargin = 10;

layout.addVIEw(tv, param2);

setContentVIEw(layout);

//---------------------------------------

myMediaPlayer my_player = new myMediaPlayer();

mPlayer = my_player.create(this, R.raw.test_cbr);

}

public void onClick(VIEw v) {

switch(v.getId()){

case 101:

mPlayer.start();

break;

case 102:

mPlayer.stop();

break;

case 103:

finish();

break;

}

}

}

Step-3: 撰寫myMediaPlayer類別,其程式碼如下:

/* myMediaPlayer.Java */

package com.misoo.pkzz;

import android.content.Context;

import android.media.MediaPlayer;

public class myMediaPlayer extends MediaPlayer {

public static MediaPlayer create(Context context){

return MediaPlayer.create(context, R.raw.test_cbr);

}

}

Step-4: 撰寫Button的子類別:myButton,其程式碼如下:

/* myButton.Java */

package com.misoo.pkcc;

import android.content.Context;

import android.widget.Button;

public class myButton extends Button {

public myButton(Context ctx){

super(ctx);

super.setBackgroundResource(R.drawable.heart);

}

public int get_width(){

return 80;

}

public int get_height(){

return 50;

}

}

Step-5: 執行之。

1.2.2.2 說明:

在ac01類別的指令:

myMediaPlayer my_player = new myMediaPlayer();

mPlayer = my_player.create(this, R.raw.test_cbr);

所傳回來的是MediaPlayer類別之物件參考(即mPlayer的值),而不是myMediaPlayer類別的物件參考。既然是MediaPlayer類別之物件參考,則外界的類別或函數(如ac01類別)就能透過mPlayer參考(即使用MediaPlayer介面)來使用此新誕生的MediaPlayer類別之物件了,如下圖:

clip_image039

雖然在my_player物件裡也有一個MediaPlayer的小物件,但是mPlayer並不是參考到它,而是參考到由create()函數所誕生的心物件。

1.2.3 範例程式(2):採繼承與委託混合型

1.2.3.1 撰寫步驟:
Step-1: 建立android專案:Px02。

Step-2: 撰寫Activity的子類別:ac01,其程式碼如下:

/* ac01.Java */

package com.misoo.pkzz;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.VIEw;

import android.view.VIEw.OnClickListener;

import android.widget.LinearLayout;

import android.widget.TextVIEw;

public class ac01 extends Activity implements OnClickListener {

private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

private final int FP = LinearLayout.LayoutParams.FILL_PARENT;

private myMediaPlayer my_player;

private myButton btn, btn2, btn3;

public TextVIEw tv;

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

LinearLayout layout = new LinearLayout(this);

layout.setOrIEntation(LinearLayout.VERTICAL);

btn = new myButton(this);

btn.setId(101);

btn.setText("play");

btn.setOnClickListener(this);

LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(btn.get_width(),

btn.get_height());

param.topMargin = 10;

layout.addVIEw(btn, param);

btn2 = new myButton(this);

btn2.setId(102);

btn2.setText("stop");

btn2.setOnClickListener(this);

layout.addVIEw(btn2, param);

btn3 = new myButton(this);

btn3.setId(103);

btn3.setText("exit");

btn3.setOnClickListener(this);

layout.addVIEw(btn3, param);

tv = new TextVIEw(this);

tv.setTextColor(Color.WHITE);

tv.setText("Ready");

LinearLayout.LayoutParams param2 =

new LinearLayout.LayoutParams(FP, WC);

param2.topMargin = 10;

layout.addVIEw(tv, param2);

setContentVIEw(layout);

//---------------------------------------

my_player = myMediaPlayer.create(this);

}

public void onClick(VIEw v) {

switch(v.getId()){

case 101:

my_player.start();

break;

case 102:

my_player.stop();

break;

case 103:

finish();

break;

}

}

}

Step-3: 撰寫myMediaPlayer類別,其程式碼如下:

/* myMediaPlayer.Java */

package com.misoo.pkzz;

import android.app.Activity;

import android.content.Context;

import android.media.MediaPlayer;

import android.util.Log;

public class myMediaPlayer extends MediaPlayer {

private static MediaPlayer mSuper;

private static Context ctx;

public static myMediaPlayer create(Context context){

ctx = context;

mSuper = MediaPlayer.create(context, R.raw.test_cbr);

return new myMediaPlayer();

}

public void start(){

try{

mSuper.start();

((ac01)ctx).tv.setText("Playing audio...");

((Activity)ctx).setTitle("MP3 Music");

} catch (Exception e) {

Log.e("StartPlay", "error: " + e.getMessage(), e);

}

}

public void stop(){

if (mSuper != null) {

((ac01)ctx).tv.setText("Stop");

mSuper.stop();

mSuper.release();

mSuper = null;

}

}

}

Step-4: 撰寫Button的子類別:myButton,其程式碼如下:

/* myButton.Java */

package com.misoo.pkcc;

import android.content.Context;

import android.widget.Button;

public class myButton extends Button {

public myButton(Context ctx){

super(ctx);

super.setBackgroundResource(R.drawable.heart);

}

public int get_width(){

return 80;

}

public int get_height(){

return 50;

}

}

Step-5: 執行之。

1.2.3.2 說明:

在ac01物件裡的my_player是參考到myMediaPlayer的物件,此物件裡mSuper再參考到MediaPlayer的物件,如下圖:

clip_image040

在ac01類別裡的指令:

my_player.start();

是呼叫到myMediaPlayer子類別的start()函數,然後才委託呼叫MediaPlayer的start()函數。此時,可看到「繼承的副作用」了,就是:myMediaPlayer繼承了眾多函數,並無法透過my_palyer去呼叫之,甚至可能是有害的。因此宜改用下一小節的單純委託方法。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved